为什么ActualSize没有更新其对WPF的价值?其对、价值、ActualSize、WPF

由网友(酒友共饮老酒)分享简介:我有以下的code:我有以下的code:

<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>

我期望看到的文本框列列1的宽度值,但所有我能看到的是0。

I expected to see at the textbox the value of the width of the column Column1 but all I can see is 0.

我见过这和this但到处说,仅涉及的Silverlight,WPF不

I've seen this and this but everywhere says that is related only to Silverlight, not WPF.

修改 修正了一个错字。另外要注意的是,输出窗口不显示任何有约束力的问题。什么是奇怪,我是它工作在设计师。它停止工作只有在运行时。

Edit Fixed a typo. Also to note that the Output window do not show any binding issue . What is strange to me is that it is working in the designer. It stops working only on runtime.

推荐答案

这是不是可以循环引用?的列1的宽度取决于文本文本框文本文本框是依赖于列1的宽度。我看不出WPF可以永远可能解决一个值,除非你明确地设置的任一列1或宽度你文本框

Wouldn't this be a circular reference? The width of "Column1" is dependent on the Text of your TextBox, and the Text of your TextBox is dependent on the width of "Column1". I don't see how WPF could ever possibly resolve a value for this unless you explicitly set the width of either "Column1" or your TextBox.

修改

呵呵,我看这个问题。 ActualWidth的不是一个依赖项属性,所以你永远不会接收更新,当值被计算出来的。

Oh, I see the problem. ActualWidth is a double not a dependency property, so you will never receive an update when the value gets calculated.

您需要使用将在您的文本框所占用的空间虚拟控制的@Robert利维的建议并绑定到 ActualWidth的来代替。

You need to use @Robert Levy's suggestion of putting a dummy control in the space occupied by your TextBox and bind to the ActualWidth of that instead.