为什么 BindingExpression 类型的附加属性在放置在 DataTemplate 中时不会被求值?

发布于 2024-10-23 22:53:32 字数 841 浏览 5 评论 0原文

我已经为 BindingExpression 类型的 Selector 定义了一个附加属性,以便捕获绑定,以便我可以将其克隆到多个属性上。当直接在 XAML 中定义选择器时,附加属性可以正常工作。但是,当在 DataTemplate 内定义选择器时,属性更改处理程序永远不会被触发。

附加属性

public static readonly DependencyProperty EnumBindingProperty = DependencyProperty.RegisterAttached(
    "EnumBinding", typeof(BindingExpression), typeof(SelectorHelper),
    new PropertyMetadata(null, OnEnumBindingChanged));

使用(有效)

<ComboBox AttachedProperties:SelectorHelper.EnumBinding="{Binding Phase}" />

使用(无效)

<DataTemplate>
    <ComboBox AttachedProperties:SelectorHelper.EnumBinding="{Binding Phase}" />
</DataTemplate>

看来这是一个计时问题,因为我发现如果我使用 Snoop评估属性时,它会自动刷新并开始工作。

任何帮助或解释将不胜感激。 :)

I have defined an attached property for Selector of type BindingExpression in order to catch a binding so I can clone it onto multiple properties. The attached property works fine when the Selector is defined directly in XAML. However when the Selector is defined inside a DataTemplate, the property changed handler never gets fired.

Attached Property

public static readonly DependencyProperty EnumBindingProperty = DependencyProperty.RegisterAttached(
    "EnumBinding", typeof(BindingExpression), typeof(SelectorHelper),
    new PropertyMetadata(null, OnEnumBindingChanged));

Usage (valid)

<ComboBox AttachedProperties:SelectorHelper.EnumBinding="{Binding Phase}" />

Usage (invalid)

<DataTemplate>
    <ComboBox AttachedProperties:SelectorHelper.EnumBinding="{Binding Phase}" />
</DataTemplate>

It seems like it is a timing issue, because I've found that if I use Snoop to evaluate the property it automatically refreshes and begins to work.

Any help or explanation would be greatly appreciated. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2024-10-30 22:53:32

看起来绑定的求值只是被尽可能长地延迟,因为表达式只是被存储为表达式而不是求值。您可以通过将附加属性类型更改为 object 来强制进行计算。

public static readonly DependencyProperty EnumBindingProperty = DependencyProperty.RegisterAttached(
    "EnumBinding", typeof(object), typeof(SelectorHelper),
    new PropertyMetadata(null, OnEnumBindingChanged));

这将使绑定尽快进行评估。然后,您可以在设置时从属性中提取 BindingExpression 并使用它,而不是现在直接设置的 BindingExpression 值。

BindingOperations.GetBindingExpression(dObj, EnumBindingProperty)

It looks like the evaluation of the binding is just being delayed as long as possible since the expression is just being stored as an expression and not evaluated. You can force the evaluation by changing the attached property type to object.

public static readonly DependencyProperty EnumBindingProperty = DependencyProperty.RegisterAttached(
    "EnumBinding", typeof(object), typeof(SelectorHelper),
    new PropertyMetadata(null, OnEnumBindingChanged));

This will cause the binding to evaluate as soon as possible. You can then extract the BindingExpression from the property when it's set and use that instead of the directly set BindingExpression value that's there now.

BindingOperations.GetBindingExpression(dObj, EnumBindingProperty)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文