views:

81

answers:

1

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.

A: 

I was able to achieve what (I believe) you're looking for by giving the TextBox a name, and binding the width of the UserControl to the ActualWidth of the TextBox. Here's the code for the Window's Grid:

<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" x:Name="entryTextBox" />
    <local:UserControl1 Grid.Row="2"
                        Width="{Binding ElementName=entryTextBox, Path=ActualWidth}"
                        HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>

Hope that helps!

Eric
That helps a lot, thanks and I flagged the question as answered. In the real context, I cannot use a Textbox (or it needs to be hidden, but I don't feel like doing that) and as the column will contain multiple controls, I won't be able to bind to a in particular. I've read other threads about binding to the gid ColumnDefinition, but this was not successful, so I'm still looking for this possibility.
742