Search code examples
c#monomacreactiveui

How to use sub classes of NSTextField and NSTableCellView with ReactiveUI


I'm currently converting the SideBarDemo to C# and MonoMac in combination with ReactiveUI. I have two subclasses of NSTextField and NSTableCellView that are views for a common view model class. My problem is that I do not know how to implement these sub classes so that data binding works. How does a good implementation of such subclasses looks like?

In the following you can see my current state. I know that the binding, that is created in the constructor won't work, because ViewModel is an ordinary property. However, I could not figure out which interfaces I should implement best.

[Register("MainCellView")]
public class MainCellView : NSTableCellView, IViewFor<TreeItemViewModel>
{
    public MainCellView ()
    {
        this.OneWayBind (ViewModel, x => x.Name, x => x.TextField.StringValue);
    }

    public MainCellView(IntPtr ptr) : base(ptr) { }

    public TreeItemViewModel ViewModel { get; set; }

    object IViewFor.ViewModel
    {
        get { return this.ViewModel; }
        set { this.ViewModel = (TreeItemViewModel)value; }
    }
}

[Register("HeaderCellView")]
public class HeaderCellView : NSTextField, IViewFor<TreeItemViewModel>
{
    public HeaderCellView () 
    {
        this.OneWayBind (ViewModel, x => x.Name, x => x.StringValue);
    }

    public HeaderCellView(IntPtr ptr) : base(ptr) { }

    TreeItemViewModel _vm;

    public TreeItemViewModel ViewModel { get; set }

    object IViewFor.ViewModel
    {
        get { return this.ViewModel; }
        set { this.ViewModel = (TreeItemViewModel)value; }
    }
}

Thx a lot in advance,

Jens


Solution

  • However, I could not figure out which interfaces I should implement best.

    If ReactiveUI doesn't have a built-in subclass that helps you out for a class, you should implement INotifyPropertyChanged for your class, and signal when ViewModel changes. That should be enough to get bindings working!