C# WPF调整网格行中控件的大小以填充所有宽度

bweufnob  于 2023-03-31  发布在  C#
关注(0)|答案(4)|浏览(319)

我正在尝试拉伸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>
jecbmhm3

jecbmhm31#

use HorizontalAlignment=“拉伸”
请参见以下代码

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

6mzjoqzu2#

好吧,如果你想让工具栏完全崩溃,我找到的唯一解决办法是写一点代码。只使用XAML布局会更优雅,但有时C#代码是唯一的方法。
1.摆脱网格:
1.处理画布的大小调整:

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
希望我能理解你的愿望,这对你有帮助,问候

yyhrrdl8

yyhrrdl83#

只需更改列宽定义:

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

默认情况下,ComboBox的水平和垂直对齐设置为“拉伸”。
对于第一列,您可以设置一个硬编码大小。
但是如果你的GUI需要更大的空间来变得更好,在ToolbarTray和ComboBox周围放一些边距
问候

bf1o4zei

bf1o4zei4#

最简单的方法是例如:在某行中使用更多列的TextBlock是使用Grid.ColumnSpan=“7”。这非常适合这样的情况:
XAML View Sample

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="70" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="60" />
        <ColumnDefinition Width="60" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.ColumnSpan="7" Text="{translation:Translate PDFExportSetResolution}" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5" TextWrapping="NoWrap" Width="Auto" />

相关问题