Probably not the implementation which you are looking for but if you really want that behavior you could always define an event handler to respond to your Grid SizeChanged
event, and update the Line X2
property in code-behind:
<Grid x:Name="grid" BorderBrush="#FFE02121" Margin="20" Background="#FFD69696" SizeChanged="grid_SizeChanged">
<Line x:Name="myline" Stroke="White" Y1="20" Y2="20" X1="0" X2="{Binding ActualWidth, ElementName=grid, Mode=OneTime}" />
</Grid>
Update your Line
in the EventHandler,
private void grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
myline.X2 = grid.ActualWidth;
}
Not only wasn't I obtaining any kind of binding error expression, but binding to the ActualWidth
dependency property was not working for other controls, such as TextBox
, for instance.
It became more apparent that the reason was pointing to a particular behaviour of the ActualWidth dependency property, rather than anything else.
While checking out the ActualWidth
documentation (ActualWidth) on the FrameworkElement class, found evidence which supports this approach.
Although it has an ActualWidthProperty backing field, ActualWidth does
not raise property change notifications and it should be thought of as
a regular CLR property and not a dependency property.
...
For purposes of ElementName binding, ActualWidth does not post updates
when it changes (due to its asynchronous and run-time calculated
nature). Do not attempt to use ActualWidth as a binding source for an
ElementName binding. If you have a scenario that requires updates
based on ActualWidth, use a SizeChanged handler.
Looking back onto the Binding
definition above, I changed the Binding Mode from its Default OneWay
to OneTime
, to only calculate the initial dimension for the X2
property when the UI is first shown, since there is no reason to "waste" resources trying to "observe" an ActualWidth
value modification when at the source we will never be signaled of any modification.
Or you could always, set the initial value in code-behind
as well.