2012-06-28 33 views
2

有沒有什麼辦法可以在.net win窗體中獲得自定義綁定行爲?自定義綁定.net形式

例如,我正在我的控制連接到BindingSource的對象,並添加綁定像

this.slider.DataBindings.Add(new System.Windows.Forms.Binding("Enabled", this.bindingSourceModel, "FloatProperty > 0.5f", true)); 

有沒有辦法,上面會工作,但我想如果dataSource.FloatProperty變得大於0.5就啓用F。

有沒有辦法做到這一點?

回答

1

我明白你想要做什麼,所以我稍微修改您的情況爲示範的目的:在UI設置是顯而易見的,有一個TrackBarButton,這裏的問題是對Enabled屬性綁定將button設置爲表達式trackBar.Value > 50的布爾值。

這個想法是把主窗體變成類似ViewModel的東西(就像在MVVM中一樣)。注意到我正在實施INotifyPropertyChanged

enter image description here enter image description here

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更新更加瘋狂。但是你因爲瘋狂而獲得了信譽,不過! ;-)