1
votes

I'm creating a WinForms application with a DataGridView. The DataSource is a ReactiveList. Adding new items to the list however does not update the UI.

ViewModel

public class HomeViewModel: ReactiveObject
{
    public ReactiveCommand<object> AddCmd { get; private set; }

    ReactiveList<Model> _models;
    public ReactiveList<Model> Models
    {
        get { return _models; }
        set { this.RaiseAndSetIfChanged(ref _models, value); }
    }

    public HomeViewModel()
    {
        Models = new ReactiveList<Model>() { new Model { Name = "John" } };

        AddCmd = ReactiveCommand.Create();
        AddCmd.ObserveOn(RxApp.MainThreadScheduler);
        AddCmd.Subscribe( _ =>
        {
            Models.Add(new Model { Name = "Martha" });
        });
    }
}

public class Model
{
    public string Name { get; set; }
}

View

public partial class HomeView : Form, IViewFor<HomeViewModel>
{
    public HomeView()
    {
        InitializeComponent();
        VM = new HomeViewModel();

        this.OneWayBind(VM, x => x.Models, x => x.gvData.DataSource);
        this.BindCommand(VM, x => x.AddCmd, x => x.cmdAdd);
    }

    public HomeViewModel VM { get; set; }

    object IViewFor.ViewModel
    {
        get { return VM; }
        set { VM = (HomeViewModel)value; }
    }

    HomeViewModel IViewFor<HomeViewModel>.ViewModel
    {
        get { return VM; }
        set { VM = value; }
    }
}
  1. The view always show "John".
  2. Debugging Subscribe show added items.
  3. Tried it with ObservableCollection same result.How to use ReactiveList so UI is updated when new items are added
  4. Tried it with IReactiveDerivedList same result. Does ReactiveUI RaiseAndSetIfChanged fire for List<T> Add, Delete, Modify?
1
Your commands could be simplified btw.. to like AddCmd = ReactiveCommand.Create(() => Models.Add(.....));Glenn Watson

1 Answers

1
votes

I think what you want is a ReactiveBindingList rather than a ReactiveList. This is a WinForms specific version of the ReactiveList for binding purposes.