Using MahApps Icons with ContextMenu

耗尽温柔 提交于 2019-12-10 16:48:25

问题


I am writing a WPF application using the MahApps Metro UI toolkit.

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

From the guide on their website, I have basically finished my application and it looks slick. The only issue I have is I have not been able to find out how to use the icon packs they provide as contextmenu icons.

Here's an visual example of what I'm trying to do. While I was able to get the original "Windows" menu item to display it's icon, I am unable to do the same for the contextmenu menuitems. Is there something I am doing wrong or a way to work around this?

Here's my .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>

And my .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;
    }
}

回答1:


I got it to work in my application using:

<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>



回答2:


I got it working by using a vector icon directly without having to embed the image or change the resource file.

<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>

The vector icon comes from materialdesignicons.com. You can select an icon and get the Path for XAML directly from there.



来源:https://stackoverflow.com/questions/35671346/using-mahapps-icons-with-contextmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!