Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to ColumnDefinition.ActualWidth returns 0

Paste this into 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 TextBlock shows 0, but shouldn't, right? Workaround?

like image 464
CannibalSmith Avatar asked Oct 20 '25 19:10

CannibalSmith


2 Answers

No, you didn't. ColumnDefinition does have an ActualWidth property, actually. But it's neither DependencyProperty nor there INotifyPropertyChanged implementation. So nobody let your target know when ActualWidth is updated.

Workaround? For what? You can always use ElementName binding to the parent FrameworkElement, who has exactly that ActualWidth that you wanted. Sorry for complex phrase :). Couldn't make it simpler.

Hope this helps.

like image 164
Anvaka Avatar answered Oct 23 '25 23:10

Anvaka


The ColumnDefinition.ActualWidth property is not a dependency property and you binding will only show it's initial value which is 0 before the grid has been updated for layout. The Grid.ActualWidth property on the other hand is a dependency property and the binding will get updated as the width changes.

like image 40
Martin Liversage Avatar answered Oct 23 '25 23:10

Martin Liversage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!