ObservableCollection< T>在Winforms和可能的替代方案中 [英] ObservableCollection<T> in Winforms and possible alternatives

查看:95
本文介绍了ObservableCollection< T>在Winforms和可能的替代方案中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Winforms .net 3.5应用程序.在我的应用中,我有一个通用类,如下所示:

Winforms .net 3.5 app. In my app I have a generic class that looks like so:

public class FilterItem {
  public FilterItem() { }
  public string FilterProperty { get; set; }
  public bool FilterPropertyChecked { get; set; }
  public ComparitiveOperator FilterOperator { get; set; }
  public string FilterValue { get; set; }
}

,当我想实现某种过滤功能时,我会在所有对话框中使用它.因此,我使用在构造函数中传递的预先填充的List<FilterItem>来调用对话框表单.现在,当对话框加载时,将遍历每个列表项并添加:

and I use it in all of my dialog boxes when I want to implement some sort of filter functionality. So I call the dialog form with a pre-poulated List<FilterItem> passed in the constructor. Now when the dialog loads, it iterates thru each list-item and adds:

  1. 一个复选框
  2. 组合框
  3. 文本框

到TableLayoutPanel中的每一行.复选框从FilterProperty获取其text属性,从FilterPropertyChecked获取其Checked状态...组合框从FilterOperator获取其绑定...文本框从FilterValue获取其文本值.

to every row in a TableLayoutPanel. The Checkbox gets its text property from FilterProperty and its Checked status from FilterPropertyChecked...the Combobox gets its binding from FilterOperator...and the Textbox gets its text value from FilterValue.

请注意我只是说 得到 .我想做的是,当绑定了其属性的控件发生更改时,它们会自动更新这些属性.我听说过ObservableCollection<T>,但是在添加System.Collections.ObjectModel名称空间后,我似乎在Winforms中没有对其的访问".

Notice how Im only saying gets. What I would like to do is automatically update these properties when the Controls whose properties they are bound to change. I heard about ObservableCollection<T> but I dont seem to have 'access' to it in Winforms after adding the System.Collections.ObjectModel namespace.

实现此目标的最佳方法是什么.具有INotifyPropertyChanged的BindingList?我不是后者的专家,如果能以这种方式行事,我将不胜感激某些指点.

What would be the best way to accomplish this. BindingList with INotifyPropertyChanged?? Im not an expert with the latter, and would greatly appreciate some pointers - if this is the way I should be going.

谢谢你!

好,让我发布一些代码以显示我认为应该做的事情:).我知道我需要在我的FilterItem类上实现INotifyPropertyChanged,所以(仅适用于FilterValue部分):

Ok, let me post some code to show what I think I should be doing :). I know I need to implement INotifyPropertyChanged on my FilterItem class, so (just for the FilterValue part for example):

public class FilterItem : INotifyPropertyChanged {
    public FilterItem() { }
    public string FilterProperty { get; set; }
    public bool FilterPropertyChecked { get; set; }
    public ComparitiveOperator FilterOperator { get; set; }

    private string _FilterValue;
    public string FilterValue {
        get { return this._FilterValue; }
        set {
            if (this._FilterValue != value) {
                this._FilterValue = value;
                this.OnFilterValueChanged();
            }
        }
    }

    #region INotifyPropertyChanged Members
    protected void OnFilterValueChanged() {
        var handler = this.PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs("FilterValue"));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}

现在应该全部合并到我的Form_Load中(这仅用于Textbox部分,而我省略了Checbox和ComboBox),如下所示:

Now it should all come together in my Form_Load (this is just for the Textbox part and I have omitted the Checbox and ComboBox) like so:

private List<FilterItem> FilterList; // <-- this gets assigned to in the constructor

private void dlgFilterData_Load(object sender, EventArgs e) {
    foreach (FilterItem item in FilterList) {
        txt = new TextBox();
        txt.DataBindings.Add("Text", item, "FilterValue", false, DataSourceUpdateMode.OnPropertyChanged);
        txt.Dock = DockStyle.Fill;
    }
}

文本框的数据绑定数据源是FilterItem'item'.但是现在我的Visual Studio IDE似乎有问题,因此无法尝试,但在启动并运行时会遇到问题.我现在想知道的是:此设置是否可以成功帮助我的各个FilterItem在其分配的控件的各自属性发生更改时自动进行更新?

the textbox's databindings DataSource is the FilterItem 'item'. But now I seem to be having a problem with my visual studio IDE, so cant try this out, but will when I have it up and running. What I would like to know now is: will this setup successfully assist in allowing my individual FilterItems to get automatically updated when their assigned Control's respective property changes??

推荐答案

ObservableCollection类位于WindowsBase程序集中,因此您需要将WindowsBase添加到项目引用中以访问"它.

The ObservableCollection class is in the WindowsBase assembly, so you'd need to add WindowsBase to your project references to "access" it.

话虽这么说,我认为ObservableCollection不会给您想要的一切.添加或删除项目时,绑定控件将得到通知,但没有自动属性更改通知,这听起来像您想要的.

That being said, I don't think ObservableCollection is going to give you everything you want. Your bound control will get notified when you add or remove items, but there is no automatic property change notification, which is what it sounds like you want.

无论您最终使用哪种集合,我都认为INotifyPropertyChanged在正确的轨道上.这是实现INotifyPropertyChanged的简单示例:

Regardless of what sort of collection you end up using, I think you're on the right track with INotifyPropertyChanged. Here's a simple example of implementing INotifyPropertyChanged:

class Foo : INotifyPropertyChanged
{
    #region Bar property

    private string _bar;
    public string Bar
    {
        get { return _bar; }
        set
        {
            if (_bar != value)
            {
                _bar = value;

                OnPropertyChanged("Bar");
            }
        }
    }

    #endregion

    protected void OnPropertyChanged(string name)
    {
        var handler = PropertyChanged;

        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

基本上,只要您的对象的属性发生更改,就触发PropertyChanged事件,并传递更改后的属性的名称.

Basically, any time a property of your object changes, fire the PropertyChanged event, passing the name of the property that changed.

最后,所有这些可能都没有意义,因为您说过要更改对象属性的值以响应对绑定到该属性的控件的更改.无需INotifyPropertyChanged或任何其他接口的帮助,通常的Windows Forms数据绑定基础结构就可以实现这一目标.仅在需要从其他位置将对象属性的更改通知绑定控件时,才需要实现INotifyPropertyChanged.

Finally, all of this may be moot, because you said that you want to change the value of an object's property in response to a change to a control bound to that property. The usual Windows Forms data binding infrastructure can make that happen without the help of INotifyPropertyChanged or any other interface. You only need to implement INotifyPropertyChanged if you need to notify a bound control of changes to your object's properties from elsewhere.

这篇关于ObservableCollection&lt; T&gt;在Winforms和可能的替代方案中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆