4

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
CannibalSmith
  • 4,742
  • 10
  • 44
  • 52

3 Answers3

7

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Anvaka
  • 15,658
  • 2
  • 47
  • 56
4

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.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • Agreed. It worked when I gave the ColumnDefinition a name and sub-UserControl a name and then assigned in the Loaded event "subUserControl.ParentColumnWidth = colDefName.ActualWidth;" – AndresRohrAtlasInformatik Aug 18 '20 at 12:37
2

From the MSDN page:

When you add or remove rows or columns, the ActualWidth for all ColumnDefinition elements and the ActualHeight of all RowDefinition elements becomes zero until Measure is called.

It would seem that the value is being bound before Measure is being called, somehow. Try forcing Mode = OneWay on your binding, and checking manually in code as well, to confirm this behaviour.

Will Eddins
  • 13,628
  • 5
  • 51
  • 85
Noldorin
  • 144,213
  • 56
  • 264
  • 302