【问题标题】:UWP: binding via ElementName is not updating uiUWP:通过 ElementName 绑定不会更新 ui
【发布时间】:2020-10-27 10:01:17
【问题描述】:

我有一个简单的Page 和一个TextBlock,我想在其中显示它的ActualHeight 和ActualWidth。

<Page
    x:Class="HaproPlace2.Views.Calibration.CalibrationView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="1080"
    d:DesignWidth="1920"
    x:Name="Page1">
    <Grid>
        <TextBlock Text="{Binding ElementName=Page1, Path=ActualHeight}" Foreground="LimeGreen" />
    </Grid>
</Page>

当app 启动时,它会显示正确的值,但在调整page/window 的大小时不会更新。如果通过ElementName 绑定到dependencyProperty,uwp 中有什么特别之处可以让它自动更新ui?

【问题讨论】:

    标签: data-binding uwp uwp-xaml


    【解决方案1】:

    见注释here。

    虽然它 [ActualHeight] 有一个 ActualHeightProperty 支持字段,但 ActualHeight 不会引发属性更改通知,它应该被视为常规 CLR 属性而不是依赖属性。

    所以你的选择是:

    • 订阅页面的SizeChanged 并从那里更新您的文本
    • 创建和使用附加属性(基本相同,只是隐藏在附加属性中)

    【讨论】:

      【解决方案2】:

      您没有使用Mode=TwoWay,为了不断更新值而强制使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-16
        • 2021-08-20
        • 1970-01-01
        • 2016-12-21
        • 2018-08-10
        • 1970-01-01
        • 2021-04-06
        • 2018-06-21
        相关资源
        最近更新 更多