【问题标题】:Binding to ElementName depends on binding order绑定到 ElementName 取决于绑定顺序
【发布时间】:2018-10-09 09:00:38
【问题描述】:

我创建了简单的StateTrigger 来绑定到FrameworkElement Width 属性。存在三个依赖属性:MinValueMaxValueElement。它们的类型分别为doubledoubleFrameworkElement

我注意到,根据绑定顺序,它可能有效,也可能无效。

这很好用。

<local:ElementWidthTrigger MaxValue="1000"
                           MinValue="800"
                           Element="{Binding ElementName=LayoutRoot}" />

这不是。

<local:ElementWidthTrigger Element="{Binding ElementName=LayoutRoot}"
                           MaxValue="1000"
                           MinValue="800" />

注意到这两个示例的唯一区别是Element 属性绑定顺序。

ElementWidthTrigger 中,每个依赖属性都有属性更改回调。当Element 在最顶部时,无论如何都不会调用回调。

x:Bind 解决了这个问题,无论使用什么顺序,但问题仍然存在。谁能解释为什么 Element 属性无法根据绑定顺序进行绑定?

在 Windows 10 1803 build 17134.320 上运行。

可以在here找到工作示例项目。

【问题讨论】:

  • 写得真好!

标签: xaml binding uwp visualstates xbind


【解决方案1】:

感谢您报告此问题并提供可靠的重现项目 - 这是一个平台错误。我已将其记录在我们的数据库中并分配给正确的团队。再次感谢!

【讨论】:

    【解决方案2】:

    哇,这是一个谜!虽然我无法找出问题的原因,但它确实看起来像是 UWP 中的一个错误。我注意到,将x:Name 添加到VisualStates 之一的简单行为甚至可以使ElementName-first 订单按预期工作:

    <VisualState x:Name="Test">
        <VisualState.StateTriggers>
            <local:ElementWidthTrigger Element="{Binding ElementName=LayoutRoot}"
                                       MaxValue="1000"
                                       MinValue="800" />
        </VisualState.StateTriggers>
    
        <VisualState.Setters>
            <Setter Target="Rectangle.Fill" Value="Blue" />
        </VisualState.Setters>
    </VisualState>
    

    如果 UWP 团队的某个人看到了这一点并提供了帮助,那就太好了,因为它可能需要深入了解内部情况才能了解正在发生的事情。

    【讨论】:

    • 好发现!没有使用x:Name 测试案例。看起来是模板化控件的一个很好的临时修复。
    猜你喜欢
    • 2012-02-25
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2012-07-14
    相关资源
    最近更新 更多