- ericbowman
- Posts: 8
- Joined:
DataBinding in Unreal
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!
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!
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: DataBinding in Unreal
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:
Then all the properties exposed in the ViewModel can be accessed from the xaml using bindings:
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:
Code: Select all
<UserControl x:Class="Tests.MyControl" ...> ... </UserControl>
Code: Select all
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")
};
Code: Select all
<TextBlock Text="{Binding PlayerName}"/>
- ericbowman
- Posts: 8
- Joined:
Re: DataBinding in Unreal
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?
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?
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: DataBinding in Unreal
PlayerName property should be defined in your ViewModel and exposed to the xaml using our reflection macros as explained in our DataBinding tutorial.Where is the "PlayerName" property defined?
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: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?
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.
Re: DataBinding in Unreal
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:
The tutorial actually only specifies how to set the data context in XAML as in here, but I got an error saying:
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:
Code: Select all
SetDataContext(Noesis::MakePtr<CustomViewModel>());
Thanks.Can't assign property to abstract class 'CustomViewModel'.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: DataBinding in Unreal
Hi Huskinu,
Anyway, setting the viewmodel as DataContext in XAML breaks one of the cool things about MVVM: decouple Model from View.
Where SomeData is a property exposed by the MainWindow view model of the type CustomViewModel.
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.I wonder why don't you make it available at NoesisSDK/Include, so a client UE project/plugin could utilize it?
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?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'."
Code: Select all
Noesis::RegisterComponent<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: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>());
Code: Select all
<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>
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 32 guests