【问题标题】:Winforms-Reactiveui 6.5 Binding to ListBoxWinforms-Reactiveui 6.5 绑定到列表框
【发布时间】:2016-09-23 06:45:05
【问题描述】:

我正在开发一个小应用程序,使用 ReactiveUI(6.5,迄今为止的最新版本)使用 WinForms 测试 MVVM 模式。我在命令(ReactiveCommand)和属性和文本框之间的一些绑定方面取得了一些进展。 我现在正试图将 ReactiveList 项目绑定到列表框(我的目的是在将元素添加到列表后自动更新列表框,并查看列表框内的新元素)。

代码如下:

视图模型:

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);
        }
}

主窗体

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; }
        }
}

对此有什么想法吗?我的意图是在不同的控件中使用它,以便与列表一起使用(dataGrids、listViews、listBoxes 等),并希望有一种方法可以做到这一点,就像使用文本框一样。

【问题讨论】:

    标签: c# winforms mvvm listbox reactiveui


    【解决方案1】:

    您应该使用ReactiveBindingList 而不是ReactiveList

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多