2012-06-28 35 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更新更加疯狂。但是你因为疯狂而获得了信誉,不过! ;-)