views:

436

answers:

1

Hey there!

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

+1  A: 

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.

Daniel Pratt
Daniel, thank u ever so much for your reply and code!In response to the above, Im not too sure I fully understand your last sentence - could u please clarify this for me? Also, am I on the right track with the textbox databinding, and using BindingList instead of List? (Sorry my VS IDE still on the blink, but I figured that its whole bunch of silly add-ins thats causing it to crash. removing them now, so hopefully will get a chance tt test all of this).
Shalan
When you set up a two-way data binding (the normal case) between an object and a control, the framework will try to update one in response to changes in the other. The databinding framework already knows how to detect when a property of a built-in control changes, so it can change the bound property of the data source object without any further help from you. If, on the other hand, you are changing a property value of the data source object in code, the framework does not know how to detect that (and update the bound control) unless you correctly implement INotifyPropertyChanged.
Daniel Pratt
Regarding using BindingList vs List: Considering how you are building up your form, it makes no difference. If you were binding your collection of FilterItem objects directly to something like a grid or listbox, BindingList would make it so that adding an item to the collection would add the same item to the grid/listbox and vice versa.
Daniel Pratt
Woah! just recovered from 10hours without connectivity! Thanks for all the input, Daniel! im pleased to tell you that going the route I described above definitely worked out for me, with some tweaks from your code ;) thanks for the help dude, I fully understand your last 2 comments!
Shalan