以编程方式添加MahApps.Metro上下文菜单

时间:2016-10-16 21:45:18

标签: c# wpf menu mahapps.metro programmatically-created

因为我正在将动态内容加载到StackPanel我想在每个对象上添加ContextMenu

现在我正在使用System.Windows.Controls.ContextMenu进行此操作。

不幸的是,菜单看起来像旧的窗体形式,而不像MahApps的漂亮菜单。

在Internet页面上,我可以找到如何在xaml中添加菜单的文档,而不是如何通过代码创建菜单。

你有什么想法吗?

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

MahApps.Metro中查找我发现ContextMenu的Style具有密钥MetroContextMenu

所以你可以在CodeBehind中创建正常的ContextMenu。您可以将以下Style添加到App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!--  MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive!  -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ContextMenu.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <!--  Accent and AppTheme setting  -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Green.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <!-- Apply MetroStyle for ContextMenu to ContextMenus in Application scope -->
    <Style TargetType="ContextMenu" BasedOn="{StaticResource MetroContextMenu}" />
</Application.Resources>

只有您必须申请ContextMenu。 MahApps没有提供特殊的ContextMenu控件(只有Style!)。