我想在我的 wpf 用户控件中使用 Visual Brush 作为上下文菜单中的图标( TreeView 的)。
我有一个资源字典(单独的图标 xaml 文件),文件中的几行如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<VisualBrush x:Key="Trashcan"
Stretch="Uniform">
我已经合并了usercontrol中的字典,并且还检查了它是否已经添加并且可以在usercontrol xaml代码中访问。
usercontrol xaml( View )中的代码如下,其中必须使用图标
<ContextMenu x:Key=xxxxxxxx>
<MenuItem Header="Delete" Command="{Binding xxxxxx, Source=xxxxxxx}" CommandParameter="{Binding}" IsEnabled="xxxxxxxxxx}" Icon="{StaticResource Trashcan}"/>
</ContextMenu>
现在的问题是我无法在上下文菜单中看到图标,图片如下:
到目前为止,我已经尝试了此链接中给出的方法 Using MahApps Icons with ContextMenu但对我来说不太管用。
除了上述链接中的方法之外,是否还有其他方法可用于在上下文菜单中将视觉画笔显示为图标。
注意:我不能使用 menuitem.icon -> 图像,因为我有来自应用程序其他组件的限制。
注意:我不知道在这里声明 Form 托管我的 WPF 用户控件 是否重要。
最佳答案
您需要为 MenuItem.Icon
设置一个 Image
。
<MenuItem.Icon>
<Image Style="{StaticResource Trashcan}"/>
</MenuItem.Icon>
在资源字典中为该图像定义样式:
<Style x:Key="Trashcan" TargetType="Image">
<Setter Property="Source" Value="/ProjectName;component/Images/Trashcan.png"/>
<Setter Property="Width" Value="24"/>
</Style>
添加:
如果您不会使用 MenuItem.Icon
,您可以使用以下技巧:
<MenuItem.Header>
<Grid>
<TextBlock Text="........"/>
<Image HorizontalAlignment="Left" Width="24" Margin="-24,0,0,0"
Style="{StaticResource Trashcan}"/>
</Grid>
</MenuItem.Header>
关于c# - 视觉画笔作为 ContextMenu wpf 中的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50553101/