在ContextMenu中使用MahApps图标

时间:2016-02-27 15:07:13

标签: c# wpf contextmenu mahapps.metro

我正在使用MahApps Metro UI工具包编写WPF应用程序。

http://mahapps.com/guides/quick-start.html

从他们网站上的指南,我基本完成了我的应用程序,它看起来很光滑。我唯一的问题是我无法找到如何使用他们提供的图标包作为上下文菜单。

enter image description here

这是我尝试做的一个直观的例子。虽然我能够得到原始的" Windows"菜单项显示它的图标,我无法对上下文菜单项执行相同操作。有什么我做错了或解决这个问题的方法吗?

这是我的.xaml:

<Menu IsMainMenu="True">
    <MenuItem Header="_Windows" ContextMenuService.IsEnabled="False" Click="WindowsMenuItem_Click">
        <MenuItem.Icon>
            <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}">
                <Rectangle.OpacityMask>
                    <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_window}" />
                </Rectangle.OpacityMask>
            </Rectangle>
        </MenuItem.Icon>
        <MenuItem.ContextMenu>
            <ContextMenu>
                <MenuItem Header="_Welcome Module">
                    <MenuItem.Icon>
                        <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_home}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Schedule Module">
                    <MenuItem.Icon>
                        <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_calendar}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Performance Module">
                    <MenuItem.Icon>
                        <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_table}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Audit Module">
                    <MenuItem.Icon>
                        <Rectangle Width="15" Height="15" Fill="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}, Path=Foreground}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_graph_line}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </MenuItem.ContextMenu>
    </MenuItem>
</Menu>

我的.xaml.cs:

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
    }

    private void WindowsMenuItem_Click(object sender, RoutedEventArgs e)
    {
        (sender as MenuItem).ContextMenu.IsEnabled = true;
        (sender as MenuItem).ContextMenu.PlacementTarget = (sender as MenuItem);
        (sender as MenuItem).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
        (sender as MenuItem).ContextMenu.IsOpen = true;
    }
}

2 个答案:

答案 0 :(得分:6)

我使用以下方法在我的应用程序中工作:

<Window.Resources>
   <ResourceDictionary>
      <VisualBrush x:Key="RunAllTestsIcon" Visual="{StaticResource appbar_list_check}"/>
   </ResourceDictionary>
</Window.Resources>
... 
<ContextMenu StaysOpen="True">
   <MenuItem Header="RunAllTests">
      <MenuItem.Icon>
         <Rectangle Width="22" Height="22" Fill="{StaticResource RunAllTestsIcon}"/>
      </MenuItem.Icon>
   </MenuItem>
</ContextMenu>

答案 1 :(得分:1)

我可以直接使用矢量图标来工作,而无需嵌入图像或更改资源文件。

//Configuration-1
@EnableJpaRepositories(basePackages = {"COMMON_REPO" })...

//Configuraton-2 same as Configuration-1
@EnableJpaRepositories(basePackages = {"CLIENT_REPO" })... 

// COMMON repo bean is not getting constructed.

Error: Error creating bean with name 'workflowRepository': Cannot create inner bean '(inner bean)#60410fb3' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#60410fb3': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

矢量图标来自materialdesignicons.com。您可以选择一个图标,然后直接从那里获取XAML的<MenuItem Header="_Reload Data" Click="alleTaken22_Click"> <MenuItem.Icon> <Viewbox Width="22" Height="22"> <Canvas Width="24" Height="24"> <Path Data="M12,5V1L7,6L12,11V7A6,6 0 0,1 18,13A6,6 0 0,1 12,19A6,6 0 0,1 6,13H4A8,8 0 0,0 12,21A8,8 0 0,0 20,13A8,8 0 0,0 12,5Z" Fill="#FFFFFF" /> </Canvas> </Viewbox> </MenuItem.Icon> </MenuItem>

enter image description here