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!
 
User avatar
sfernandez
Site Admin
Posts: 2983
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: 2983
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.
 
Huskinu
Posts: 12
Joined: 15 Mar 2021, 05:40

Re: DataBinding in Unreal

12 Apr 2021, 18:12

Hi sfernandez,

In DataBinding tutorial, there mentions a class named NotifyPropertyChangedBase, which currently resides at NoesisSDK/Src/Packages/App/ApplicationLauncher/Include/NsApp/NotifyPropertyChangedBase.h.
I wonder why don't you make it available at NoesisSDK/Include, so a client UE project/plugin could utilize it?

Besides, it is really worth mentioning that if we have a CustomControl inherit UserControl with its data be bound to a CustomViewModel (inheriting NotifyPropertyChangedBase), so in CustomControl's ctor, we need to call:
SetDataContext(Noesis::MakePtr<CustomViewModel>());
The tutorial actually only specifies how to set the data context in XAML as in here, but I got an error saying:
Can't assign property to abstract class 'CustomViewModel'.
Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: DataBinding in Unreal

14 Apr 2021, 11:42

Hi Huskinu,
I wonder why don't you make it available at NoesisSDK/Include, so a client UE project/plugin could utilize it?
NotifyPropertyChangedBase is not part of our core library, we offer it with source code as part of our application framework so users can include it their projects or use it as reference to create their own.
The tutorial actually only specifies how to set the data context in XAML as in here, but I got an error saying: "Can't assign property to abstract class 'CustomViewModel'."
To use the view model in xaml you need to register it in module startup like you do with the user controls, have you done that?
Noesis::RegisterComponent<CustomViewModel>();
Anyway, setting the viewmodel as DataContext in XAML breaks one of the cool things about MVVM: decouple Model from View.
Besides, it is really worth mentioning that if we have a CustomControl inherit UserControl with its data be bound to a CustomViewModel (inheriting NotifyPropertyChangedBase), so in CustomControl's ctor, we need to call: SetDataContext(Noesis::MakePtr<CustomViewModel>());
It can be created by the custom control (as you mentioned) or be part of a bigger view model set in your main window:
<UserControl x:Class="Test.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Test">
  ...
  <local:CustomControl DataContext="{Binding SomeData}"/>
</UserControl>
Where SomeData is a property exposed by the MainWindow view model of the type CustomViewModel.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 39 guests