【问题标题】:{Binding ElementName=...} does not work from any CompositeTransform property{Binding ElementName=...} 不适用于任何 CompositeTransform 属性
【发布时间】:2015-04-01 20:50:14
【问题描述】:

我有以下代码,其中我在主网格 (LayoutRoot) 下隐藏了一个 WebView,以便稍后可以制作滑动动画:

<Page...>
    <Grid x:Name="LayoutRoot">
        ...
        <Grid x:Name="ContentRoot">
            ...
        </Grid>
        <WebView...>
            <WebView.RenderTransform>
                <CompositeTransform TranslateY="{Binding ElementName=LayoutRoot, 
                                    Path=ActualHeight}"/> <!--Does not work-->
            </WebView.RenderTransform>
        </WebView> 
    </Grid>
</Page>

当我第一次在设计器中键入{Binding ElementName=...} 行时,WebView 会出现在 Grid 的正下方,就像它应该的那样。但是,当我重新构建解决方案或运行应用程序时,WebView 只会遮盖整个 LayoutRoot。

无论我绑定什么/无论控件是什么,都会发生这种情况;但是,绑定到 exact 相同的表达式将在设计器和手机中正确显示。为了证明我在说什么:

<Button Width="{Binding ElementName=LayoutRoot, Path=ActualHeight}"> <!--Works perfectly, both on designer and phone-->
    <Button.RenderTransform>
        <CompositeTransform SomeProperty={Binding ElementName=SomeElement, Path=SomePath}"/> <!--This does not work-->
    </Button.RenderTransform>
</Button>

除了为此编写 C# 代码之外,还有什么方法可以绑定到 LayoutRoot.ActualHeight

【问题讨论】:

    标签: xaml data-binding windows-phone-8.1 winrt-xaml elementname


    【解决方案1】:

    您遇到的一个问题是您尝试绑定到ActualHeight,它既不是依赖属性也不是可观察的 (INotifyPropertyChanged) 属性,因此绑定仅在首次创建时评估一次。

    【讨论】:

    • 那么,为什么控件的其他属性(例如Height或Width)能够毫无问题地绑定到Grid的ActualHeight?
    • 他们不应该那样做。也许它们的绑定在第一次布局传递之后评估,而转换的绑定在之前评估。
    • ActualHeight 一个依赖属性。 msdn.microsoft.com/en-us/library/…
    • @KooKiz 抱歉,如果我在描述中没有充分说明:我正在制作一个 WinRT 应用程序。您提供的链接是针对 WPF 的。
    • 这是一个奇怪的属性,ActualWidth 的 MSDN 页面说它是“计算属性”并且“不要尝试使用 ActualWidth 作为 ElementName 绑定的绑定源。如果你有一个需要基于ActualWidth 进行更新的场景,请使用SizeChanged 处理程序。"
    猜你喜欢
    • 1970-01-01
    • 2016-05-02
    • 2020-04-30
    • 2022-06-15
    • 1970-01-01
    • 2010-10-25
    • 2021-08-31
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多