绑定到ColumnDefinition.ActualWidth无效

时间:2012-02-05 05:45:25

标签: c# wpf xaml data-binding grid

我正在尝试绑定到列的ActualWidth但绑定似乎不起作用。在以下示例中,带有绑定的TextBlock始终为0,但通过代码更新的TextBlock会报告正确的数字。发生了什么事?

XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" Name="MainColumn" />
    </Grid.ColumnDefinitions>
    <StackPanel>
        <TextBlock Text="From Binding:" />
        <TextBlock Text="{Binding ElementName=MainColumn, Path=ActualWidth}" />
        <TextBlock Text="From Code:" />
        <TextBlock Text="" Name="WidthFromCodeTextBlock" />
    </StackPanel>
    <GridSplitter HorizontalAlignment="Right" Width="5" DragDelta="GridSplitter_DragDelta" />
</Grid>

代码:

private void GridSplitter_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
    WidthFromCodeTextBlock.Text = MainColumn.ActualWidth.ToString();
}

1 个答案:

答案 0 :(得分:1)

不是依赖项属性,因此没有绑定更新。

(另外,如果你不use them,为什么还有专栏?)