【问题标题】:C# WPF Resizing Controls In Grid Row to Fill All WidthC# WPF 调整网格行中控件的大小以填充所有宽度
【发布时间】:2016-01-10 21:22:03
【问题描述】:

我正在尝试让我的 ComboBox 拉伸以使用展开窗口中的所有空白空间。如果窗口的宽度被缩小,ToolBarTray 将会折叠。

我使用了一个有 2 列的网格,其宽度分别指定为 * 和 300。 如何使 ComboBox 填充行中的所有可用空间?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>

    <DockPanel LastChildFill="True" Grid.Row="0">
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="300" />
            </Grid.ColumnDefinitions>

            <ToolBarTray DockPanel.Dock="Left">
                <ToolBar Name="Standard">
                    <Button Height="32">
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">
                            <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />
                        </StackPanel>
                    </Button>

                    <Button Height="32">
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">
                            <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />
                        </StackPanel>
                    </Button>

                    <Button Height="32">
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">
                            <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />
                        </StackPanel>
                    </Button>
                </ToolBar>

                <ToolBar Name="Standard2">
                    <Button Height="32">
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">
                            <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />
                        </StackPanel>
                    </Button>

                    <Button Height="32">
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">
                            <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />
                        </StackPanel>
                    </Button>

                    <Button Height="32">
                        <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right">
                            <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" />
                        </StackPanel>
                    </Button>
                </ToolBar>
            </ToolBarTray>

            <ComboBox Grid.Column="1" MinWidth="200" ></ComboBox>
        </Grid>
    </DockPanel>
</Grid>

【问题讨论】:

    标签: wpf grid resize width row


    【解决方案1】:

    使用 Horizo​​ntalAlignment="Strech"

    见以下代码

    <ComboBox Grid.Column="1" MinWidth="200" HorizontalAlignment="Stretch"></ComboBox>
    

    【讨论】:

      【解决方案2】:

      好的,如果您希望工具栏完全折叠,我找到的唯一解决方案是编写一点代码。 仅在 XAML 中进行布局会更优雅,但有时​​ C# 代码是唯一的方法。

      1. 摆脱网格:

      2. 处理画布的大小调整:

        private void ToolbarSizeChanged(object sender, SizeChangedEventArgs e)
        {
            const double COMBO_MIN_DESIRED_WIDTH = 300.0;
            Size newSize = e.NewSize;
        
            Size sizeToolbarTray = new Size(Double.PositiveInfinity, newSize.Height);
            // measure to update DesiredSize
            toolBarTray.Measure(sizeToolbarTray);
        
            Rect toolBarTrayRect = new Rect(0, 0, 0, newSize.Height);
            Rect comboRect =new Rect(0,0,0,newSize.Height);
        
            // 3 cases :
        
            // 1) Much space is available
            if (newSize.Width > toolBarTray.DesiredSize.Width + COMBO_MIN_DESIRED_WIDTH)
            {
                toolBarTrayRect.Width = toolBarTray.DesiredSize.Width;
                comboRect.X = toolBarTrayRect.Width;
                comboRect.Width = newSize.Width -toolBarTrayRect.Width;
        
            }
            // 2) Space to show Combo, but not totally toolbar
            else if (newSize.Width > COMBO_MIN_DESIRED_WIDTH)
            {
                toolBarTrayRect.Width = newSize.Width - COMBO_MIN_DESIRED_WIDTH;
                comboRect.X = toolBarTrayRect.Width;
                comboRect.Width = COMBO_MIN_DESIRED_WIDTH;
            }
            // 3) Not enough space to show toolbar
            else
            {
                toolBarTrayRect.Width = 0;
                comboRect.Width = newSize.Width;
            }
            // Layout the two components :
            toolBarTray.Arrange(toolBarTrayRect);
            combobox.Arrange(comboRect);
        }
        

      这是一个工作解决方案的链接,以便您检查它是否是您想要的:
      http://1drv.ms/1MySnde

      希望我能理解您的意愿,并且对您有所帮助,问候

      【讨论】:

        【解决方案3】:

        只需更改您的列宽定义:

            <Grid DockPanel.Dock="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
        

        默认情况下,ComboBox 的水平和垂直对齐方式设置为 Stretch。

        可能是您可以放置​​硬编码大小的第一列。

        但如果您的 GUI 需要更多空间才能更好看,请在 ToolbarTray 和 ComboBox 周围放置一些边距

        问候

        【讨论】:

        • 使用Width="Auto 可防止 ToolBarTray 正确折叠。最终我所做的是为各个列指定Width="*" MaxWidth="400"Width="*" MinWidth="500"。它是一种解决方法,我必须找到 ToolBarTray 的绝对宽度,比如 400 并设置为 MaxWidth。不是最好的方法,因为向 ToolBarTray 添加新项目意味着再次找到绝对宽度。希望有更好的解决方案。
        • 我用我的解决方案尝试了你的代码,看起来还不错。我可以调整工具栏的大小,右边没有多余的空间。究竟什么是崩溃问题?这是我的解决方案1drv.ms/1LInfDa 用于比较
        • link 这是我的示例解决方案。随着窗口宽度的减小,ComboBox 推动 ToolBarTray,折叠按钮。
        • 好的,我下载了你的代码,对我来说没关系,我不会改变任何东西! ;-) 是你想要的东西:“组合框至少 300 dpi
        猜你喜欢
        • 1970-01-01
        • 2014-01-16
        • 2011-01-17
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        • 2013-12-22
        • 2012-08-10
        • 1970-01-01
        相关资源
        最近更新 更多