Hello,
I can't figure out how to manage properly the width of a grid column with a user control in one of its cell. I have this xaml for a window:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Content="Button" Width="100" HorizontalAlignment="Left"/>
<TextBox Grid.Row="1" />
<local:UserControl1 Grid.Row="2"
HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
and the user control is a drawing set to stretch as needed:
<UserControl x:Class="WpfApplication1.UserControl1"
Height="auto" Width="auto">
<Grid Background="Aqua">
<Path Fill="Coral" Stroke="Black" StrokeThickness="1"
Stretch="Uniform"
HorizontalAlignment="Left" VerticalAlignment="Top">
<Path.Data>
<RectangleGeometry Rect="40,20, 140,30" RadiusX="10" RadiusY="10" />
</Path.Data>
</Path>
</Grid>
</UserControl>
To help I have set the user control background in blue.
I would like to have the width of the column ignoring the "natural" size of the user control. I mean I would like the user control to not determine the width during the layout of the column, but to adjust its width to whatever is the width of the column.
In the example, initially the Grid would set the column width to the value of the button, and the user control would use the column width to resize itself. Then if a long text is entered in the Textbox, as soon as the Textbox starts to be wider than the button, and the column starts to be resized as well, in turn the user control would adjust to maintain the same size than the column.
I've tried combinations of stretch values, and also have used MeasureOverride() in the user control. The latter doesn't work because the AvalaibleSize received is Infinity, not the column width.