阻止网格中的控件更改其列宽

742*_*742 2 wpf grid layout resize

我无法弄清楚如何在其一个单元格中使用用户控件正确管理网格列的宽度.我有一个窗口的xaml:

<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>
Run Code Online (Sandbox Code Playgroud)

并且用户控件是根据需要拉伸的绘图集:

<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>
Run Code Online (Sandbox Code Playgroud)

为了帮助我将用户控件背景设置为蓝色.

我希望列的宽度忽略用户控件的"自然"大小.我的意思是我希望用户控件在列布局期间不确定宽度,而是将其宽度调整为列的宽度.

在该示例中,最初Grid将列宽设置为按钮的值,用户控件将使用列宽来调整自身大小.然后,如果在文本框中输入长文本,只要文本框开始比按钮宽,并且列也开始调整大小,反过来用户控件将调整为保持与列相​​同的大小.

我尝试过拉伸值的组合,并且还在用户控件中使用了MeasureOverride().后者不起作用,因为收到的AvalaibleSize是Infinity,而不是列宽.

Eri*_*son 7

我是能够实现你通过为文本框的名称,并绑定该用户控件的TextBox的ActualWidth的宽度找什么(我相信)进行.这是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" x:Name="entryTextBox" />
    <local:UserControl1 Grid.Row="2"
                        Width="{Binding ElementName=entryTextBox, Path=ActualWidth}"
                        HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!