我正在尝试绑定到列的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();
}