簡體   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?

我正在嘗試制作一個簡單的 WPF 應用程序,其中包含可填充可用寬度的部分。 盡管嘗試了各種方法來拉伸元素、容器和子項的寬度,但沒有任何效果,我不知道為什么。

另一個問題說使用uniformgrid效果很好,除了它統一設置所有元素的高度,這絕對不是我想要的。 我希望所有部分看起來都像圖片中的部分 - 根據內容填充寬度、高度自動。 這是基本設置:

<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