gpt4 book ai didi

data-binding - UWP - 将元素绑定(bind)到主窗口大小并随着窗口大小的变化更新值

转载 作者:行者123 更新时间:2023-12-04 05:09:05 26 4
gpt4 key购买 nike

我想将窗口的当前大小绑定(bind)到文本 block 。在当前的实现中,主窗口的大小是在运行时设置的,但是如果我在应用程序启动后调整窗口大小,新的大小不会在文本 block 中更新。

<Grid x:Name="grid" Background="#FFE8E8E8">
<TextBox x:Name="textBoxSample" Width="300" Height="200" Text="{Binding ActualWidth, ElementName=grid}"></TextBox>
</Grid>

最佳答案

您不应绑定(bind)到 ActualWidth。 FrameworkElement.ActualWidth的备注文档说:

Although it has an ActualWidthProperty backing field, ActualWidth does not raise property change notifications and it should be thought of as a regular CLR property and not a dependency property.

For purposes of ElementName binding, ActualWidth does not post updates when it changes (due to its asynchronous and run-time calculated nature). Do not attempt to use ActualWidth as a binding source for an ElementName binding. If you have a scenario that requires updates based on ActualWidth, use a SizeChanged handler.

您将需要一些其他方法来确定窗口的大小,例如通过订阅 SizeChanged 事件。

关于data-binding - UWP - 将元素绑定(bind)到主窗口大小并随着窗口大小的变化更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40525260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com