Wpf 拉伸和中心对齐?

Wpf 拉伸和中心对齐?,wpf,xaml,vertical-alignment,Wpf,Xaml,Vertical Alignment,我正在使用一个水平的列表框制作一种导航栏(可能有更好的方法),原因是我需要一次只能选择一件东西,并保持选中状态,然后才能取消选中。因此,为了实现这一点,我使用了一个ToggleButton 问题是,我将内容“拉伸”到适合框中,这样我就可以单击选择区域中的任意位置,使用切换按钮取消选择 我的问题是,由于拉伸,我的按钮现在是顶部对齐的,在使用拉伸时是否可以将它们垂直居中?每当我尝试同时将它们居中时,它会使我回到原点,按钮本身会再次收缩到中心 <ListBox Grid.Row="0" Grid

我正在使用一个水平的
列表框制作一种导航栏(可能有更好的方法),原因是我需要一次只能选择一件东西,并保持选中状态,然后才能取消选中。因此,为了实现这一点,我使用了一个
ToggleButton

问题是,我将内容“拉伸”到适合框中,这样我就可以单击选择区域中的任意位置,使用切换按钮取消选择

我的问题是,由于拉伸,我的按钮现在是顶部对齐的,在使用拉伸时是否可以将它们垂直居中?每当我尝试同时将它们居中时,它会使我回到原点,按钮本身会再次收缩到中心

<ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding PageViewModels}" SelectedItem="{Binding CurrentPageViewModel}" Style="{StaticResource MenuStyle}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#FCCC"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="#FAAA"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                <ToggleButton VerticalAlignment="Stretch"
                            Content="{Binding Name}"
                            IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                            Style="{StaticResource BlankButtonStyle}"
                            />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


这可能是一个非常愚蠢的问题,但它确实困扰着我,我有点卡住了。

您可以使用VerticalContentAlignment属性

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    ...

...

所以我可以通过简单地向ListItem模板添加一个TextBlock来解决这个问题

我可以将现在空白的ToggleButton拉伸到ListItem的完整大小,然后对TextBlock进行居中/任意操作

这不是一个理想的实现,但它是目前我能想到的最好的实现

<ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" />
            <ToggleButton VerticalAlignment="Stretch"
                        Content=""
                        IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                        Style="{StaticResource BlankButtonStyle}"
                        />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>