ObservableCollection<T> in Winforms and possible alternatives

14,453

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

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.

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

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

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.

Share:
14,453
Shalan
Author by

Shalan

Updated on September 03, 2022

Comments

  • Shalan
    Shalan almost 2 years

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

    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. A checkbox
    2. A combobox
    3. A textbox

    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.

    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.

    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.

    thank u!

    EDIT:

    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
    }
    

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

    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??