gpt4 book ai didi

c# - 将用户控件绑定(bind)到 bool 属性的对面

转载 作者:可可西里 更新时间:2023-11-01 03:14:25 26 4
gpt4 key购买 nike

非常简单:我希望做与 this 相同的事情但在 winforms 中。谷歌似乎提取的所有内容都是特定于 wpf 的(即,我不想引用 presentationframework.dll)

如果您不想阅读链接,请解释:

以下是我想做的事情的意图的表示,尽管它显然行不通。

CheckBox1.DataBindings.Add(new Binding("Checked", this.object, "!SomeBool"));

最佳答案

你有两个选择:

  1. 手动创建Binding 对象并附加到FormatParse事件并交换每个事件中的值。
  2. 在类上创建一个附加属性,该属性只是反转预期属性的逻辑

第一个选项更简洁,IMO,因为它不会强制您的类的 API 遵循您的 UI 设计,尽管第二个选项(略微)更容易。

选项 1 的示例

private void SwitchBool(object sender, ConvertEventArgs e)
{
e.Value = !((bool)e.Value);
}

...

Binding bind = new Binding("Checked", this.object, "SomeBool");

bind.Format += SwitchBool;
bind.Parse += SwitchBool;

CheckBox1.DataBindings.Add(bind);

选项 2 的示例

public class SomeClass
{
public bool SomeBool { get; set; }

public bool NotSomeBool
{
get { return !SomeBool; }
set { SomeBool = !value; }
}
}

...

CheckBox1.DataBindings.Add("Checked", this.object, "NotSomeBool");

同样,我非常赞成选项 1,因为选项 2 要求您根据您的 UI 设计定制您的类。

关于c# - 将用户控件绑定(bind)到 bool 属性的对面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2464102/

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