Wpf 将菜单项的内容置于菜单控件的中心

Wpf 将菜单项的内容置于菜单控件的中心,wpf,xaml,menu,menuitem,Wpf,Xaml,Menu,Menuitem,我有一个相当愚蠢的问题,我无法解决。 对于我的项目,我使用WPF(和MVVM),并希望有一个好看的垂直对齐的菜单控件。 但是我无法集中我的菜单项的内容 以下是我的菜单的XAML代码: <Menu ItemsSource="{Binding Commands}" ItemTemplate="{StaticResource CommandsTemplate}" ItemContainerStyle="{StaticResource CommandsStyle}"&g

我有一个相当愚蠢的问题,我无法解决。 对于我的项目,我使用WPF(和MVVM),并希望有一个好看的垂直对齐的
菜单
控件。
但是我无法集中我的
菜单项的内容

以下是我的菜单的XAML代码:

<Menu ItemsSource="{Binding Commands}" 
      ItemTemplate="{StaticResource CommandsTemplate}" 
      ItemContainerStyle="{StaticResource CommandsStyle}">
    <Menu.ItemsPanel>
        <ItemsPanelTemplate>
             <VirtualizingStackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </Menu.ItemsPanel>
</Menu>
可悲的是,这让在按钮上悬停变得非常难看

之前:

之后:

所以这仍然不是我想要的

回答

我几乎要放弃了,并试图在我的
MenuItem
中做其他设计工作。其中一个正在改变它的悬停颜色。我找到了一个帖子。然后我将
HorizontalAlignment=“Center”
添加到我的
StackPanel
中,现在它可以工作了

以下是更新的代码:

<Style x:Key="CommandsStyle" TargetType="MenuItem">
    <Setter Property="Command" Value="{Binding Command}"/>
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Border x:Name="Bd"  BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" 
                        SnapsToDevicePixels="True" 
                        Uid="Border_38">
                    <StackPanel HorizontalAlignment="Center" Margin="4">
                        <Image Width="32" Source="{Binding IconSource}"/>
                        <TextBlock Text="{Binding Path=DisplayName}" />
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="True">
                        <Setter Property="Background" TargetName="Bd" Value="LightGray"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="Gray"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我将鼠标悬停在菜单项上时的样子:


将StackPanel更改为Grid,并为文本和图像添加列。文本的列宽应为
*
,图像的列宽应为
自动
,然后将
文本块
水平对齐到
中心
。嘿,我发现问题可能是堆栈面板。请使用以下参数进行测试:。如果这不起作用,我想你需要嵌套的堆栈面板。我用这些参数试过了,但仍然不起作用。你能给我举一个如何使用嵌套堆栈面板的例子吗?因为我们没有看到所有的XAML代码,所以很难说是什么导致了这个问题。你检查过“菜单”的边距了吗?我正要回答,这时我看到你已经解决了这个问题!:)在Twitter上找我(我有相同的用户名)。将StackPanel更改为Grid,并为文本和图像添加列。文本的列宽应为
*
,图像的列宽应为
自动
,然后将
文本块
水平对齐到
中心
。嘿,我发现问题可能是堆栈面板。请使用以下参数进行测试:。如果这不起作用,我想你需要嵌套的堆栈面板。我用这些参数试过了,但仍然不起作用。你能给我举一个如何使用嵌套堆栈面板的例子吗?因为我们没有看到所有的XAML代码,所以很难说是什么导致了这个问题。你检查过“菜单”的边距了吗?我正要回答,这时我看到你已经解决了这个问题!:)在Twitter上寻找我(我有相同的用户名)。
<Setter Property="HorizontalAlignment" Value="Center"/>
<Style x:Key="CommandsStyle" TargetType="MenuItem">
    <Setter Property="Command" Value="{Binding Command}"/>
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Border x:Name="Bd"  BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" 
                        SnapsToDevicePixels="True" 
                        Uid="Border_38">
                    <StackPanel HorizontalAlignment="Center" Margin="4">
                        <Image Width="32" Source="{Binding IconSource}"/>
                        <TextBlock Text="{Binding Path=DisplayName}" />
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="True">
                        <Setter Property="Background" TargetName="Bd" Value="LightGray"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="Gray"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>