1

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?

2 Answers2

1

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

Glenn Watson
  • 2,758
  • 1
  • 20
  • 30
  • Thanks, but. if i use ReactiveBindingList i cant share my ViewModels and ReactiveBindingList dont work on modification like `Models[0].Name = "Palma";` – arturogranillo Oct 03 '17 at 22:28
  • Well, you can derive a WinformsVM from your initial VM, that creates a bunch of CreateDerviedBindingLists()'s on your main VM's ReactiveList's, that way you can keep your logic shared between platforms. – Glenn Watson Oct 03 '17 at 22:33
0

You should use BindingList.

reference :

"If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. For example, if you have a combo box bound to an ArrayList and data is added to the ArrayList, these new items will not appear in the combo box. However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound." https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-a-windows-forms-combobox-or-listbox-control-to-data?view=netframeworkdesktop-4.8

Or

ReactiveBindingList

It work fine for me. !!!

suwet
  • 1