为什么 BindingExpression 类型的附加属性在放置在 DataTemplate 中时不会被求值?
我已经为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来绑定的求值只是被尽可能长地延迟,因为表达式只是被存储为表达式而不是求值。您可以通过将附加属性类型更改为
object
来强制进行计算。这将使绑定尽快进行评估。然后,您可以在设置时从属性中提取 BindingExpression 并使用它,而不是现在直接设置的 BindingExpression 值。
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
.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.