User avatar
aliagha.huseynli
Topic Author
Posts: 44
Joined: 29 Mar 2023, 10:17

Data Binding issue

13 Feb 2024, 09:22

Hi Noesis team,
I have a problem with Data Binding.

I have 2 different c# scripts in Unity which contain these lines:
view = FindObjectOfType<NoesisView> ();
view.Content.DataContext = this;
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?
 
User avatar
nadjibus
Posts: 32
Joined: 24 Feb 2022, 04:09

Re: Data Binding issue

13 Feb 2024, 20:53

You may need to create a custom class (a.k.a ViewModel) containing both objects and setting it as the DataContext:
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;
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;
}
Then your XAML would be:
<TextBlock Text="{Binding FirstObject.Name}" />
<TextBlock Text="{Binding SecondObject.Name}" />
 
User avatar
aliagha.huseynli
Topic Author
Posts: 44
Joined: 29 Mar 2023, 10:17

Re: Data Binding issue

14 Feb 2024, 07:55

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.
 
User avatar
nadjibus
Posts: 32
Joined: 24 Feb 2022, 04:09

Re: Data Binding issue

14 Feb 2024, 23:40

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):
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;
}
Your ShellView (XAML):
<ContentControl Content="{Binding CurrentView}" />
Initialize your NoesisView (at the start of the game)
var noesisView = FindObjectOfType<NoesisView>();
view.Content = new ShellView { DataContext = new ShellViewModel() };
When you want to display a view:
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: Ahrefs [Bot], AnKor, Google [Bot] and 4 guests