C# Winforms Reactiveui 6.5绑定到ListBox
我正在开发一个小应用程序,用WinForms测试MVVM模式,使用ReactiveUI(6.5,目前为止的最新版本)。我在命令(ReactiveCommand)和属性与文本框之间的一些绑定方面取得了一些进展。 我现在无法将项目的反应列表绑定到列表框(我的目的是在元素添加到列表后自动更新列表框,并查看列表框中的新元素) 代码如下: 视图模型:C# Winforms Reactiveui 6.5绑定到ListBox,c#,winforms,mvvm,listbox,reactiveui,C#,Winforms,Mvvm,Listbox,Reactiveui,我正在开发一个小应用程序,用WinForms测试MVVM模式,使用ReactiveUI(6.5,目前为止的最新版本)。我在命令(ReactiveCommand)和属性与文本框之间的一些绑定方面取得了一些进展。 我现在无法将项目的反应列表绑定到列表框(我的目的是在元素添加到列表后自动更新列表框,并查看列表框中的新元素) 代码如下: 视图模型: public class PersonViewModel : ReactiveUI.ReactiveObject { (...) public Reac
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);
}
}
公共类PersonViewModel:ReactiveUI.ReactiveObject
{
(...)
公共反应列表ID{get;private set;}
公共PersonViewModel()
{
Ids=新的反应列表();
(...)
}
//在列表中添加新项的命令
私人无效添加人(内部id)
{
添加(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; }
}
}
public分部类MainForm:Form,IViewFor
{
公共表格(
{
初始化组件();
ViewModel=新的PersonViewModel();
//PersonListBox.DataSource=ViewModel.Ids;->这是一个想法,它也不起作用
当激活时(d=>
{
d(this.Bind(ViewModel,x=>x.Ids,x=>x.PersonListBox.DataSource));//绑定尝试似乎不起作用
d(这个.BindCommand(ViewModel,x=>x.AddPersonCommand,x=>x.AddPersonButton));//命令,它可以工作
});
}
公共PersonViewModel视图模型{get;set;}
对象IViewFor.ViewModel
{
获取{return ViewModel;}
设置{ViewModel=(PersonViewModel)值;}
}
}
有什么想法吗?我的目的是在不同的控件中使用它,以便与列表(数据网格、列表视图、列表框等)一起使用,希望有一种方法可以做到这一点,就像使用文本框一样。您应该使用
ReactiveBindingList
而不是ReactiveList