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:
- A checkbox
- A combobox
- 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 FilterItem
s to get automatically updated when their assigned Control's respective property changes??