gpt4 book ai didi

WPF:XAML 属性声明不是通过 Setter 设置的?

转载 作者:行者123 更新时间:2023-12-04 16:49:30 25 4
gpt4 key购买 nike

我有一个 WPF 应用程序,我在代码隐藏中使用依赖属性,我想通过 XAML 声明进行设置。

例如

<l:SelectControl StateType="A" Text="Hello"/>

所以在这个例子中我有一个 UserControl调用 SelectControl ,它有一个名为 StateType 的属性它在它的 setter 中操纵其他一些属性。

为了帮助说明问题,我放置了另一个名为 Text 的属性。在示例中,请继续阅读,我将进一步解释。

代码隐藏摘录...
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(SelectControl));

public String Text
{
get { return (String)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}

public static readonly DependencyProperty StateTypeProperty = DependencyProperty.Register("StateType", typeof(String), typeof(SelectControl));

public String StateType
{
get { return (String)GetValue(StateTypeProperty) }
set
{
switch (value)
{
case "A":
AnotherPropertyBoolean = true;
break;
case "B":
AnotherPropertyBoolean = false;
break;
default:
// this is only an example...
}
}
}

现在,如果我在 setter 上设置断点(对于 StateTypeText ),事实证明它从未执行过。

但是为 Text 声明的值,即“Hello”出现在它的数据绑定(bind) TextBox ,当然我将另一个文本控件绑定(bind)到 StateType的值(value)我也能看到。

有谁知道发生了什么?

最佳答案

依赖属性的“CLR-wrappers”只有在通过代码完成时才会被调用。 XAML 取决于 DependencyProperty.Register(...) 调用中指定的名称。因此,不要像上面那样为依赖属性“扩展”setter 的逻辑,只需将自定义逻辑放在 PropertyChangedCallback 中即可。功能。

关于WPF:XAML 属性声明不是通过 Setter 设置的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3836076/

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