WPF StackPanel 内容垂直对齐

12

XAML 中,有没有一种方法可以让我垂直居中对齐水平方向的 StackPanel 中的所有组件?

我使用以下 XAML 实现了所需的结果:

<StackPanel Orientation="Horizontal">
     <TextBlock VerticalAlignment="Center"/>
     <Button VerticalAlignment="Center"/>
     <TextBox VerticalAlignment="Center"/>
     <Button VerticalAlignment="Center"/>
     <TextBlock VerticalAlignment="Center"/>
</StackPanel>

但是我需要为每个控件单独重复 VerticalAlignment="Center"

是否有一种在StackPanel级别上声明以下内容的方法?

<StackPanel Orientation="Horizontal" VERTICALCONTENTALIGNMENT="Center">
     <TextBlock/>
     <Button/>
     <TextBox/>
     <Button/>
     <TextBlock/>
</StackPanel>
4个回答

5
StackPanel放入Grid中,并在StackPanel上设置VerticalAlignment="Center"
<Grid>
    <StackPanel VerticalAlignment="Center">
        ...
    </StackPanel
</Grid>

2
你可以使用设置所有子元素的 VerticalAlignmentTrigger 来为 StackPanel 定义样式:
<Style x:Key="HorizontalStackPanel" TargetType="{x:Type StackPanel}">
    <Setter Property="Orientation" Value="Horizontal" />
    <Style.Triggers>
        <Trigger Property="Orientation" Value="Horizontal">
            <Setter Property="FrameworkElement.VerticalAlignment"  Value="Center" />
        </Trigger>
    </Style.Triggers>
</Style>

并应用此样式:

<StackPanel Style="{StaticResource HorizontalStackPanel}">
    <TextBlock />
    <Button />
    <TextBox />
    <Button />
    <TextBlock />
</StackPanel>

它对我有效。 整个代码:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="HorizontalStackPanel" TargetType="{x:Type StackPanel}">
            <Setter Property="Orientation" Value="Horizontal" />
            <Style.Triggers>
                <Trigger Property="Orientation" Value="Horizontal">
                    <Setter Property="FrameworkElement.VerticalAlignment"  Value="Center" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Style="{StaticResource HorizontalStackPanel}">
            <TextBlock Text="One"/>
            <Button Content="Two"/>
            <TextBox Text="Three"/>
            <Button Content="Four"/>
            <TextBlock Text="Five"/>
        </StackPanel>
    </Grid>
</Window>

@Muds,你在哪里定义了HorizontalStackPanel样式? - user2250152
在资源中,看起来与其他答案完全相同的问题。 - Muds
@Muds 的资源是哪个元素?我已经包含了整个代码。 - user2250152
仅限于窗口,而不是任何单独的元素。 - Muds
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/75271/discussion-between-user2250152-and-muds。 - user2250152

1
定义一个如下的样式;
<Style x:Key="StackHorizontal" TargetType="StackPanel">
    <Style.Resources>
        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
            <Setter Property="VerticalAlignment"  Value="Center" />
        </Style>
        <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="VerticalAlignment"  Value="Center" />
        </Style>
    </Style.Resources>
</Style>

-2

只需使用这个:

<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
     <TextBlock/>
     <Button/>
     <TextBox/>
     <Button/>
     <TextBlock/>
</StackPanel>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接