wpf 在ContextMenu中使用MahApps图标

gblwokeq  于 2023-05-13  发布在  其他
关注(0)|答案(3)|浏览(163)

我正在使用MahApps Metro UI工具包编写WPF应用程序。
http://mahapps.com/guides/quick-start.html
从他们网站上的指南来看,我基本上已经完成了我的申请,看起来很光滑。唯一的问题,我有是我还没有能够找出如何使用图标包,他们提供的上下文菜单图标。

这里有一个我正在尝试的视觉例子。虽然我能够让原来的“Windows”菜单项显示它的图标,但我无法为contextmenu menuitems做同样的事情。是我做错了什么,还是有办法解决这个问题?
下面是我的.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;
    }
}
ukqbszuj

ukqbszuj1#

我让它在我的应用程序中工作,使用:

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

wlwcrazw2#

我得到了它的工作,直接使用矢量图标,而不必嵌入图像或更改资源文件。

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

矢量图标来自materialdesignicons.com。您可以选择一个图标并直接从那里获取XAML的Path

xlpyo6sf

xlpyo6sf3#

使用最新的MahApps IconPacks,您可以简单地执行以下操作:

xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
    

<MenuItem.Icon>
   <iconPacks:PackIconForkAwesome Kind="Calendar" Width="20" Height="20"/>
</MenuItem.Icon>

相关问题