Search code examples
prismuno-platformwinui

What do I need to do in my view to make Prism.Uno work with x:Bind?


I come from a WPF / Prism background but I really like what X:Bind offers. How do I get x:Bind to work with my ViewModel when using Prism.Uno?

I have prism:ViewModelLocator.AutoWireViewModel="True" but I seem to be missing something in my understanding of how it works when designing.

Thanks G


Solution

  • The use of x:Bind requires the binding path to be rooted in the View.

    To use the DataContext, you'll need make it available typed through the view, like this:

    public partial class MyControl : INotifyPropertyChanged
    {
    #if !HAS_UNO 
            // Uno already defines this event (it will be removed 
            // in the future to be aligned properly with WinUI)
            public event PropertyChangedEventHandler PropertyChanged;
    #endif
    
            public MainPage()
            {
                this.InitializeComponent();
    
                DataContextChanged += 
                    (s, e) => PropertyChanged?.Invoke(
                       this, 
                       new PropertyChangedEventArgs(nameof(ViewModel)));
            }
    
            public MyViewModel ViewModel => DataContext as MyViewModel;
    
    }