gpt4 book ai didi

c# - 自定义绑定(bind) .net 表单

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:55 25 4
gpt4 key购买 nike

有没有办法在 .net win 表单中获得自定义绑定(bind)行为?

例如,我将我的控件连接到一个 BindingSource 对象并添加一个绑定(bind),例如

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

如果 dataSource.FloatProperty 变得大于 0.5f,上面的方法将无法工作,但我喜欢启用它。

有什么办法吗?

最佳答案

我明白你想做什么,所以为了演示我稍微修改了你的情况:UI 设置很明显,有一个 TrackBar 和一个 Button,这里的问题是将 buttonEnabled 属性绑定(bind)到表达式 trackBar.Value > 50 的 bool 值。

想法是将主窗体变成类似 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;

...
}

现在,这是我个人的观察:虽然对您的目标有重要的解释,但您的目标有点疯狂。您必须考虑为什么要通过数据绑定(bind)来实现这一目标。绑定(bind)主要针对属性值的自动、双向 同步。通过直接绑定(bind)到“模型”来进行这种类型的 UI 更新更加疯狂。但是你因为疯狂而受到赞扬,尽管如此! ;-)

关于c# - 自定义绑定(bind) .net 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11238607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com