我明白你想要做什麼,所以我稍微修改您的情況爲示範的目的:在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更新更加瘋狂。但是你因爲瘋狂而獲得了信譽,不過! ;-)