Binding to ColumnDefinition.ActualWidth returns 0

Insert it into the cider.

<Grid x:Name="Grid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="ColumnDefinition"/>
    </Grid.ColumnDefinitions>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Text="{Binding ActualWidth, ElementName=Grid}"/>
        <TextBlock Text="{Binding ActualWidth, ElementName=ColumnDefinition}"/>
    </StackPanel>
</Grid>

Now run it. The second TextBlockshows 0, but should not, right? Workaround?

+3
source share
3 answers

No, you didn’t. Actually ColumnDefinition has a property ActualWidth. But this is neither an implementation DependencyPropertynor an implementation INotifyPropertyChanged. Therefore, no one lets your goal know when it is being updated ActualWidth.

? ? ElementName FrameworkElement, ActualWidth, . :). .

, .

+6

ColumnDefinition.ActualWidth , , 0, ​​ . Grid.ActualWidth, , , .

+3

MSDN:

, ActualWidth ColumnDefinition ActualHeight RowDefinition , Measure.

It would seem that the value is attached before it is Measureinvoked in any way. Try forcing Mode = OneWayin your binding and also manually checking the code to confirm this behavior.

+2
source

Source: https://habr.com/ru/post/1718726/


All Articles