Well, what I /wanted/ to do turned out messier than I planned, since the
solution I was thinking of applies mainly to an "I'll show/bind anything"
control I have locally, but how about the following C# 3 solution (the main
bit being the anonymous type/array, marked "KEY BIT")?
Marc
static class Program {
static void Main() {
// our demo object
Foo foo = new Foo();
Application.EnableVisualStyles();
using (Form form = new Form {
Controls = {
new CheckBox {
Dock = DockStyle.Top,
DataBindings = {
{ "Checked", foo, "Bar", false,
DataSourceUpdateMode.OnPropertyChanged }
}
},
new ComboBox {
Dock = DockStyle.Top,
DropDownStyle = ComboBoxStyle.DropDownList,
// *** START KEY BIT
ValueMember = "Value",
DisplayMember = "Text",
DataSource = new[] {
new {Text="Yes", Value=true},
new {Text="No", Value=false}
},
DataBindings = {
{"SelectedValue", foo, "Bar",false,
DataSourceUpdateMode.OnPropertyChanged}
}
// *** END KEY BIT
}
}
}) {
foo.PropertyChanged += delegate {
form.Text = string.Format("Bar={0}", foo.Bar);
};
Application.Run(form);
}
}
}
public class Foo : INotifyPropertyChanged {
private bool bar;
public bool Bar {
get { return bar; }
set {UpdateField(ref bar, value, "Bar");}
}
protected void UpdateField<T>(ref T field, T value, string propertyName)
{
// general purpose "if changed, update field and notify" method
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
if (PropertyChanged != null) PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
sklett - 14 Mar 2008 21:10 GMT
Hi Marc,
This slipped by me somehow, I've only just seen your response. I will check
this out later and get back to you.
Thanks for taking the time!
-Steve
> Well, what I /wanted/ to do turned out messier than I planned, since the
> solution I was thinking of applies mainly to an "I'll show/bind anything"
[quoted text clipped - 62 lines]
> public event PropertyChangedEventHandler PropertyChanged;
> }