gpt4 book ai didi

c# - UWP 中的绑定(bind)未设置附加行为属性

转载 作者:行者123 更新时间:2023-12-03 10:26:44 26 4
gpt4 key购买 nike

我创建了一个具有依赖属性的行为类,我想将它附加到我的 View (XAML) 中的控件。我正在使用 MVVM,我需要通过将它绑定(bind)到我的 ViewModel 中的一个属性来设置这个附加属性,但它没有被设置。这是我想做的简化版本:

行为类:

public static class TestBehavior
{
public static readonly DependencyProperty SomeStringProperty =
DependencyProperty.Register("SomeString", typeof(string), typeof(TestBehavior), new PropertyMetadata(""));

public static string GetSomeString(DependencyObject o)
{
return (string)o.GetValue(SomeStringProperty);
}

public static void SetSomeString(DependencyObject o, string value)
{
o.SetValue(SomeStringProperty, value);
}

}

XAML:
<TextBlock Text="{Binding ViewModelProperty}" local:TestBehavior.SomeString="{Binding ViewModelProperty}" />

TextBlock 的“Text”属性正确绑定(bind),但行为的“SomeString”属性没有正确绑定(bind)。

有趣的是 - 如果我将行为属性“硬编码”为它确实被设置的值。例如:
<TextBlock Text="{Binding TestValue}" local:TestBehavior.SomeString="Foo" /> <!-- This Works --> 

任何想法为什么绑定(bind)到行为属性不起作用?

最佳答案

你期望附加行为做什么?

您是否通过在 GetSomeString 上设置断点来确定附加属性是否有效?/SetSomeString方法?如果是这样,这将不适用于绑定(bind),因为在使用绑定(bind)时不会调用 Get/Set 方法。

如果您想在附加属性更改时使用react,无论是否通过绑定(bind),请使用 PropertyChangedCallbackPropertyMetadataRegister 中指定称呼。

关于c# - UWP 中的绑定(bind)未设置附加行为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506305/

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