I am developing a small app to test MVVM pattern with WinForms, using ReactiveUI (6.5, latest version so far). I made some progress with commands (ReactiveCommand) and some Bindings between properties and TextBoxes. I am stuck now trying to bind a ReactiveList of items to a listbox (my intention is to automatically update the listbox, once an element is added to the list, and see the new element inside the listbox).
Here the code:
ViewModel:
public class PersonViewModel : ReactiveUI.ReactiveObject
{
(...)
public ReactiveList<int> Ids { get; private set; }
public PersonViewModel ()
{
Ids = new ReactiveList<int>();
(...)
}
//The command that adds a new item inside the list
private void AddPerson(int id)
{
Ids.Add(id);
}
}
MainForm
public partial class MainForm : Form, IViewFor<PersonViewModel>
{
public MainForm()
{
InitializeComponent();
ViewModel = new PersonViewModel();
//PersonsListBox.DataSource = ViewModel.Ids; -> this was an idea, it doesn't work either
this.WhenActivated(d =>
{
d(this.Bind(ViewModel, x => x.Ids, x => x.PersonsListBox.DataSource)); // Binding attempt, doesn't seem to be working
d(this.BindCommand(ViewModel, x => x.AddPersonCommand, x => x.AddPersonButton)); // Command, it works
});
}
public PersonViewModel ViewModel { get; set; }
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (PersonViewModel)value; }
}
}
Any ideas on that? My intention is to use it in different controls that make sense to be used with lists (dataGrids, listViews, listBoxes etc.), and hopefully there is a method to do it, in the same way it is done with textboxes.