简体   繁体   English

当 Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 不起作用时,如何使 WPF 中的控件拉伸以填充可用宽度?

[英]How can I make controls in WPF stretch to fill available width when HorizontalAlignment and HorizontalContentAlignment don't work?

I'm trying to make a simple WPF app that has sections that fill the available width.我正在尝试制作一个简单的 WPF 应用程序,其中包含可填充可用宽度的部分。 Despite trying various ways of stretching the width of elements, containers, and children, nothing is working and I can't figure out why.尽管尝试了各种方法来拉伸元素、容器和子项的宽度,但没有任何效果,我不知道为什么。

Another question said to use uniformgrid which worked well EXCEPT that it set the height of all the elements uniformly which was definitely not what I wanted.另一个问题说使用uniformgrid效果很好,除了它统一设置所有元素的高度,这绝对不是我想要的。 I want all of the sections to look like the one in the picture - filled width, height auto based on the content.我希望所有部分看起来都像图片中的部分 - 根据内容填充宽度、高度自动。 Here's the basic setup:这是基本设置:

<Window x:Class="A_Customizer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:A_Customizer"
        mc:Ignorable="d"
        Title="MainWindow"
        Background="#FF2B2B2B"
        Width="800"
        >
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Cursor" Value="Hand"/>
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <Grid Name="mainApp" >

        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <WrapPanel Grid.Row="0" >
            <Button ToolTip="Click to apply the below settings to this Jumpbox" Click="ApplyCustomizations">Customize</Button>
        </WrapPanel>

        <ScrollViewer Grid.Row="1">

            <WrapPanel HorizontalAlignment="Stretch" >
                <GroupBox
                    Background="#FFE2E2E2"
                    BorderBrush="#FF7F7F7F"
                    Margin="10,10,10,10"
                    Name="pathsBox"
                    HorizontalContentAlignment="Stretch"
                    HorizontalAlignment="Stretch"
                    >
                    <GroupBox.Header>
                        <Border Background="#FFAFAFAF" CornerRadius="3">
                            <Label FontWeight="Bold">Key Paths</Label>
                        </Border>
                    </GroupBox.Header>

                    <StackPanel HorizontalAlignment="Stretch">
                        <Grid Margin="0,10,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="50"/>
                            </Grid.ColumnDefinitions>

                            <TextBox Name="homeFolder" Grid.Column="0" HorizontalAlignment="Stretch"></TextBox>
                            <Button Grid.Column="1" Click="NewQuickPath" ToolTip="Change home folder">
                                <Image Source="images\add_folder.png" Height="25" Cursor="Hand"></Image>
                            </Button>
                        </Grid>

                        <TextBox Name="progFolder" Grid.Column="0" HorizontalAlignment="Stretch"></TextBox>
                    </StackPanel>

                </GroupBox>

                <GroupBox
                    Background="#FFE2E2E2"
                    BorderBrush="#FF7F7F7F"
                    Margin="10,10,10,10"
                    Name="quickBox"
                    Height="auto"
                    HorizontalContentAlignment="Stretch"
                    >
                    <GroupBox.Header>
                        <Border Background="#FFAFAFAF" CornerRadius="3">
                            <Label FontWeight="Bold">Quick Access Folders</Label>
                        </Border>
                    </GroupBox.Header>
            
                    <StackPanel HorizontalAlignment="Stretch">
                        <TextBlock TextWrapping="Wrap" Margin="15">
                            There are going to be folders you'll need to access frequently and keeping them pinned on top of the left menu in Explorer is helpful. 
                            Select here to add them to the list of folders restored with the "Customize" button. Click any folder to remove it.
                        </TextBlock>

                        <Border CornerRadius="3" Background="#FFF3C7C7" Margin="6" Visibility="Collapsed" Name="quickErr" Tag="err_box">
                            <TextBlock Tag="errMsg" Foreground="#FFFD3434" TextWrapping="Wrap" Margin="6" ></TextBlock>
                        </Border>

                        <UniformGrid Name="quickPathsArea" Columns="1">
                    
                        </UniformGrid>
                
                        <Grid Margin="0,10,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="50"/>
                            </Grid.ColumnDefinitions>

                            <TextBox  Grid.Column="0" HorizontalAlignment="Stretch"></TextBox>
                            <Button Grid.Column="1" Click="NewQuickPath" ToolTip="Add a new folder">
                                <Image Source="images\add_folder.png" Height="25" Cursor="Hand"></Image>
                            </Button>
                        </Grid>
                    </StackPanel>
                </GroupBox>     
</wrappanel>
</scrollviewer>
</grid>

在此处输入图像描述

带有 Orientation="Vertical" (默认值)而不是WrapPanelStackPanel应该可以工作:它将允许每个子元素使用全宽和尽可能多的高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM