.net - 为什么 ActualSize 没有在 WPF 上更新其值?

标签 .net wpf binding

我有以下代码:

<Window x:Class="UnderstandSizing.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" x:Name="Column1" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Grid.Column="0" Text="{Binding ActualWidth,ElementName=Column1,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>

我希望在文本框中看到 Column1 列的宽度值,但我只能看到 0。

我看过thisthis但到处都说这仅与 Silverlight 有关,与 WPF 无关。

编辑
修正了一个错字。还要注意输出窗口不显示任何绑定(bind)问题。对我来说奇怪的是它在设计师中工作。它仅在运行时停止工作。

最佳答案

有趣的...

正如 DanM 发现的那样, ColumnDefinition.ActualWidth 不是依赖属性,因此当它更改时您不会获得绑定(bind)更新。

一种解决方法是在列中放置一个隐藏控件并绑定(bind)到它的 ActualWidth ,如下所示:

    <ContentControl Visibility="Hidden" Grid.Column="0" HorizontalAlignment="Stretch" x:Name="hidden"/>
    <TextBox Grid.Column="0" Text="{Binding ActualWidth,ElementName=hidden,Mode=OneWay}" />

关于.net - 为什么 ActualSize 没有在 WPF 上更新其值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9008525/

相关文章:

wpf - ItemsControl ItemTemplate 绑定(bind)

c# - 重命名特定类的库

.net - 为特定密码启用 TLS 1.2

wpf - WPF 应用程序中的全局 Entity Framework 上下文

c# - 在应用程序配置文件中找不到名为 '' 的连接字符串

css 验证错误 moz 绑定(bind)?

WPF Datagrid 组扩展文本 - 如何绑定(bind)?

javascript - knockout 映射——将多个数据源整合到一个 View 模型中

c# - WPF 使用 x :Name as a key 绑定(bind)到字典

c# - Datagrid 绑定(bind) (WPF) 导致 UI 延迟