绑定到ElementName取决于绑定顺序。

3
我创建了一个简单的 StateTrigger 来绑定到 FrameworkElement 的 Width 属性。它有三个依赖属性: MinValue、MaxValue 和 Element。它们的类型分别为 double、double 和 FrameworkElement。
我注意到,根据绑定顺序,它可能会或可能不会工作。
这个功能很好。
<local:ElementWidthTrigger MaxValue="1000"
                           MinValue="800"
                           Element="{Binding ElementName=LayoutRoot}" />

这是与IT技术无关的内容。

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

注意到这两个示例的唯一区别是Element属性绑定的顺序。
ElementWidthTrigger中,每个依赖属性都有一个属性更改回调。当Element位于顶部时,无论如何它的回调都不会被调用。 x:Bind解决了这个问题,无论使用什么顺序,但问题仍然存在。有人能解释一下为什么Element属性在绑定顺序上失败吗?
运行在Windows 10 1803 build 17134.320上。
可以在这里找到工作示例项目。

写得非常好! - Lynn Crumbling
2个回答

3

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


2

哇,这真是个谜!虽然我还没有找到问题的原因,但它似乎确实是UWP中的一个bug。我注意到仅仅在其中一个VisualStates上添加x:Name就可以使ElementName按照预期工作:

<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团队中的某个人看到并提供帮助,那将是很棒的事情,因为这可能需要对内部机制有深入了解才能理解发生了什么。


1
好发现!没有测试过带有“x:Name”的情况。看起来是模板控件的一个不错的临时解决方案。 - khamitimur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接