ericbowman
Topic Author
Posts: 8
Joined: 19 Feb 2020, 20:14

DataBinding in Unreal

26 Jun 2020, 18:39

I am trying to get databinding working in unreal and I'm having trouble.

First, will any of my C# code such as MainWindow.xaml.cs run in unreal or will I need to code in C++?

If I do need to code in C++, is there an Unreal binding walkthrough that explains how to wire up the xaml file to the C++ file?

Thanks!

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: DataBinding in Unreal

29 Jun 2020, 11:38

Hi,

The C# code generated in Blend can't be used in Unreal unless you are already using a plugin that allows C# projects (https://github.com/pixeltris/USharp, https://github.com/nxrighthere/UnrealCLR) although we haven't tested that path yet.

You can bind data to the xaml using Blueprint objects like we do in our GitHub samples.

Or you can create everything in C++ as explained in our DataBinding tutorial. Basically you create a code-behind class for your xaml, and connect the ViewModel when its xaml gets loaded:
<UserControl x:Class="Tests.MyControl" ...> ... </UserControl>
class MyControl : public Noesis::UserControl
{
public:
  MyControl()
  {
    Initialized() += MakeDelegate(this, &MyControl::OnInitialized);
    InitializeComponent();
  }
  
private:
  void InitializeComponent()
  {
    GUI::LoadComponent(this, "MyControl.xaml");
  }

  void OnInitialized(Noesis::BaseComponent*, const Noesis::EventArgs&)
  {
    SetDataContext(MakePtr<ViewModel>());
  }

  NS_IMPLEMENT_INLINE_REFLECTION_(MyControl, UserControl, "Tests.MyControl")
};
Then all the properties exposed in the ViewModel can be accessed from the xaml using bindings:
<TextBlock Text="{Binding PlayerName}"/>
 
ericbowman
Topic Author
Posts: 8
Joined: 19 Feb 2020, 20:14

Re: DataBinding in Unreal

02 Jul 2020, 22:12

Where is the "PlayerName" property defined?

Is the cpp file you posted the header file or the implementation file? If it's the header file, do you have a header file only?
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: DataBinding in Unreal

03 Jul 2020, 18:12

Where is the "PlayerName" property defined?
PlayerName property should be defined in your ViewModel and exposed to the xaml using our reflection macros as explained in our DataBinding tutorial.
Is the cpp file you posted the header file or the implementation file? If it's the header file, do you have a header file only?
The previous code I posted was just a simple example to show how a UserControl code behind can set a ViewModel. The class can be defined entirely in a header, or split in .h/.cpp files as you can see in our samples:
https://github.com/Noesis/Tutorials/blo ... MainMenu.h
https://github.com/Noesis/Tutorials/blo ... inMenu.cpp

I want to remark that C++ classes that are used in xaml (like user controls) should be registered when module is started so they are available when xamls are loaded:
https://github.com/Noesis/Tutorials/blo ... 3DGame.cpp

I hope this helps.

Who is online

Users browsing this forum: No registered users and 2 guests