【问题标题】:Prevent a control in a grid to change its column width防止网格中的控件更改其列宽
【发布时间】:2011-02-17 18:39:20
【问题描述】:

我不知道如何在其中一个单元格中使用用户控件正确管理网格列的宽度。我有一个用于窗口的 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>

并且用户控件是一个绘图集,可以根据需要进行拉伸:

<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>

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

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

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

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

【问题讨论】:

    标签: wpf layout grid resize


    【解决方案1】:

    我能够实现(我相信)您正在寻找的东西,方法是给 TextBox 一个名称,并将 UserControl 的宽度绑定到 TextBox 的 ActualWidth。这是窗口网格的代码:

    <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>
    

    希望有帮助!

    【讨论】:

    • 这很有帮助,谢谢,我将问题标记为已回答。在实际上下文中,我不能使用文本框(或者它需要隐藏,但我不想这样做),并且由于列将包含多个控件,我将无法绑定到特定的。我已经阅读了有关绑定到 gid ColumnDefinition 的其他线程,但这并不成功,所以我仍在寻找这种可能性。
    【解决方案2】:

    老问题,所以它可能不再帮助 OP,但它可能会帮助其他人:

    将 UserControl1 放在 Canvas 中。然后将 Width 绑定到父 Grid 的 ActualWidth 上。

    例子:

    <Grid Name="mainGrid">
              <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
              </Grid.RowDefinitions>
              <Something Grid.Row="0"/>
              <Canvas Grid.Row="1">
                    <MyControl Width="{Binding ActualWidth, ElementName=mainGrid, Mode=OneWay}"/>
              </Canvas>
    </Grid>
    

    画布将采用可用宽度,但永远不会向 Grid 询问更多。 画布内的控件无法从 Canvas 获得大小,因此您必须手动为其指定大小。在这种情况下,我们想给它父 Grid 的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 2011-05-13
      • 1970-01-01
      • 2013-05-22
      • 2021-07-01
      相关资源
      最近更新 更多