- aliagha.huseynli
- Posts: 44
- Joined:
Data Binding issue
Hi Noesis team,
I have a problem with Data Binding.
I have 2 different c# scripts in Unity which contain these lines:
It creates conflict so my binding is not working.
Only one of them is working.
Should I combine my scripts? Is there any other way to make them work separately?
I have a problem with Data Binding.
I have 2 different c# scripts in Unity which contain these lines:
Code: Select all
view = FindObjectOfType<NoesisView> ();
view.Content.DataContext = this;
Only one of them is working.
Should I combine my scripts? Is there any other way to make them work separately?
Re: Data Binding issue
You may need to create a custom class (a.k.a ViewModel) containing both objects and setting it as the DataContext:
Then your XAML would be:
Code: Select all
view = FindObjectOfType<NoesisView>();
var viewModel = view.Content.DataContext as MainViewModel;
if (viewModel = null)
view.Content.DataContext = viewModel = new MainViewModel();
viewModel.FirstObject = this; // or viewModel.SecondObject = this;
Code: Select all
public class MainViewModel : INotifyPropertyChanged
{
private YourObjectType _firstObject;
private YourObjectType _secondObject ;
public YourObjectType FirstObject { get => _firstObject; set => Set(ref _firstObject, value); }
public YourObjectType SecondObject { get => _secondObject; set => Set(ref _firstObject, value); }
private void Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Code: Select all
<TextBlock Text="{Binding FirstObject.Name}" />
<TextBlock Text="{Binding SecondObject.Name}" />
- aliagha.huseynli
- Posts: 44
- Joined:
Re: Data Binding issue
Thanks for your reply.
Unfortunately, it is not what I am looking for. I would like to create different scripts each one has its content.
Such as:
Menu script Binding value 1
Settings script Binding value 2
HUD script Binding value 3
Each script should own binding values.
So, I need to use one ViewModel.cs and Get and Set them.
Unfortunately, it is not what I am looking for. I would like to create different scripts each one has its content.
Such as:
Menu script Binding value 1
Settings script Binding value 2
HUD script Binding value 3
Each script should own binding values.
So, I need to use one ViewModel.cs and Get and Set them.
Re: Data Binding issue
In this case you need to create one view (XAML) per script (ViewModel) and assign it to NoesisView. Or better, create one main View/ViewModel responsible of switching views (a ShellView):
Your ShellView (XAML):
Initialize your NoesisView (at the start of the game)
When you want to display a view:
Code: Select all
public class ShellViewModel : INotifyPropertyChanged
{
public UserControl CurrentView { get; private set; }
public void SetView<T>(object viewModel = null) where T : UserControl
{
var view = Activator.CreateInstance<T>();
view.DataContext = viewModel;
CurrentView = view;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentView));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Code: Select all
<ContentControl Content="{Binding CurrentView}" />
Code: Select all
var noesisView = FindObjectOfType<NoesisView>();
view.Content = new ShellView { DataContext = new ShellViewModel() };
Code: Select all
var noesisView = FindObjectOfType<NoesisView>();
var shellViewModel = noesisView.Content.DataContext as ShellViewModel; // Better cache and reuse this
shellViewModel.SetView<Script1View>(script1);
Who is online
Users browsing this forum: Bing [Bot] and 8 guests