我明白你想要做什么,所以我稍微修改您的情况为示范的目的:在UI设置是显而易见的,有一个TrackBar
和Button
,这里的问题是对Enabled
属性绑定将button
设置为表达式trackBar.Value > 50
的布尔值。
这个想法是把主窗体变成类似ViewModel的东西(就像在MVVM中一样)。注意到我正在实施INotifyPropertyChanged
。
public partial class ManiacalBindingForm : Form, INotifyPropertyChanged {
public ManiacalBindingForm() {
InitializeComponent();
this.button.DataBindings.Add("Enabled", this, "ManiacalThreshold", true, DataSourceUpdateMode.OnPropertyChanged);
this.trackBar.ValueChanged += (s, e) => {
this.Text = string.Format("ManiacalBindingForm: {0}", this.trackBar.Value);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("ManiacalThreshold"));
};
}
public bool ManiacalThreshold {
get { return this.trackBar.Value > 50; }
}
public event PropertyChangedEventHandler PropertyChanged;
...
}
现在,这是我个人的观察:虽然是你的目标一个不平凡的解释,你的目标是有点疯狂。你必须思考为什么你想通过数据绑定来实现这一点。绑定主要针对自动,双向,同步属性值。通过直接绑定到“模型”来做这种类型的UI更新更加疯狂。但是你因为疯狂而获得了信誉,不过! ;-)