为什么菜单项在XAML& amp;创建时在左侧对齐WPF

时间:2016-05-04 05:24:46

标签: c# wpf xaml visual-studio-2013

在WPF中创建菜单时,它会显示左对齐的菜单项框。 是否有任何属性使其显示右侧对齐? (像Windows窗体一样)。

我一直在互联网上搜索但没有成功,感谢您提供的任何帮助。

/ * XAML CODE * /

   <Menu>
        <MenuItem Header="One">
            <MenuItem Header="One-One"></MenuItem>
            <MenuItem Header="One-Two"></MenuItem>
            <MenuItem Header="One-Three"></MenuItem>
            <MenuItem Header="One-Four"></MenuItem>
            <MenuItem Header="One-Five"></MenuItem>
        </MenuItem>
        <MenuItem Header="Two">
            <MenuItem Header="Two-One"></MenuItem>
            <MenuItem Header="Two-Two"></MenuItem>
            <MenuItem Header="Two-Three"></MenuItem>
            <MenuItem Header="Two-Four"></MenuItem>
            <MenuItem Header="Two-Five"></MenuItem>
        </MenuItem>

    </Menu>

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以像这样编辑菜单项的模板

<Window.Resources>
        <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                    <Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>
                    <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
                        <Border x:Name="SubMenuBorder" BorderBrush="#FF999999" BorderThickness="1" Background="#FFF0F0F0" Padding="2">
                            <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=SubMenuBorder}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                                    </Canvas>
                                    <!--<Rectangle Fill="#FFD7D7D7" HorizontalAlignment="Left" Margin="29,2,0,2" Width="1"/>-->
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                    <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                </Trigger>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                    <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                    <Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>
                </Trigger>
                <Trigger Property="CanContentScroll" SourceName="SubMenuScrollViewer" Value="False">
                    <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
                    <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Menu>
            <MenuItem Header="One" Template="{DynamicResource MenuItemControlTemplate1}">
                <MenuItem Header="One-One" Template="{DynamicResource MenuItemControlTemplate1}"></MenuItem>
                <MenuItem Header="One-Two"></MenuItem>
                <MenuItem Header="One-Three"></MenuItem>
                <MenuItem Header="One-Four"></MenuItem>
                <MenuItem Header="One-Five"></MenuItem>
            </MenuItem>
            <MenuItem Header="Two">
                <MenuItem Header="Two-One"></MenuItem>
                <MenuItem Header="Two-Two"></MenuItem>
                <MenuItem Header="Two-Three"></MenuItem>
                <MenuItem Header="Two-Four"></MenuItem>
                <MenuItem Header="Two-Five"></MenuItem>
            </MenuItem>

        </Menu>
    </Grid>

答案 1 :(得分:0)

对我有用的是:

    <Menu>
    <MenuItem Header="_One">
        <MenuItem Header="_One-One"></MenuItem>
        <MenuItem Header="_One-Two"></MenuItem>
        <MenuItem Header="_One-Three"></MenuItem>
        <MenuItem Header="_One-Four"></MenuItem>
        <MenuItem Header="_One-Five"></MenuItem>
    </MenuItem>
    <MenuItem Header="_Two">
        <MenuItem Header="_Two-One"></MenuItem>
        <MenuItem Header="_Two-Two"></MenuItem>
        <MenuItem Header="_Two-Three"></MenuItem>
        <MenuItem Header="_Two-Four"></MenuItem>
        <MenuItem Header="_Two-Five"></MenuItem>
    </MenuItem>

</Menu>

在名称前放置_使其成为热键CTRL +标题名称,但我的对齐也很好。

答案 2 :(得分:0)

问题出在注册表编辑器上。

这个网页解决了我的问题:

http://www.sevenforums.com/tutorials/786-menus-open-left-right-side.html

在我的情况下,只删除或设置为0 HKEY_CURRENT_USER \ Software \ Microsoft \ Windows NT \ CurrentVersion \ Windows中的“MenuDropAlignment”键解决了问题。

希望这个答案可以帮助其他人解决同样的问题。

感谢那些试图提供帮助的人。