通过样式设置器设置 MenuItem 图标
<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Icon" Value="{Binding Icon}" />
<Setter Property="Header" Value="{Binding Text}" />
<Setter Property="ItemsSource" Value="{Binding Children}" />
<Setter Property="Command" Value="{Binding Command}" />
</Style>
在这样的代码中设置它:
Uri refreshUri = new Uri("..\\Resources\\Refresh16.bmp",UriKind.Relative);
BitmapImage refreshIcon = new BitmapImage();
refreshIcon.UriSource = refreshUri;
图标没有显示,有任何线索吗?
<Style x:Key="ContextMenuItemStyle" TargetType="{x:Type MenuItem}">
<Setter Property="Icon" Value="{Binding Icon}" />
<Setter Property="Header" Value="{Binding Text}" />
<Setter Property="ItemsSource" Value="{Binding Children}" />
<Setter Property="Command" Value="{Binding Command}" />
</Style>
setting it in code like this:
Uri refreshUri = new Uri("..\\Resources\\Refresh16.bmp",UriKind.Relative);
BitmapImage refreshIcon = new BitmapImage();
refreshIcon.UriSource = refreshUri;
the Icon doesn't show up, any clues ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
refreshIcon
是Icon
属性的来源,那么您可能需要在代码示例之后调用NotifyPropertyChanged("Icon")
(并实现INotifyPropertyChanged
接口)和/或将Icon
声明为DependencyProperty
。以下是有关
INotifyPropertyChanged
界面。啊,我明白你的问题...尝试将
Icon
属性设置为Image
并绑定到Image
的源:您也可以只需将图像放入主项目中的 Images 文件夹中,然后在 xaml 中引用它,如下所示:
If the
refreshIcon
is the source of yourIcon
property, then you may need to either callNotifyPropertyChanged("Icon")
after your code example (and implement theINotifyPropertyChanged
interface) and/or declareIcon
as aDependencyProperty
.Here is a link to more information about the
INotifyPropertyChanged
interface.Ahh, I see your problem... try setting the
Icon
property to anImage
and bind to the source of theImage
:You can also just put the image into an Images folder in your main project and reference it in xaml like this:
对于仍在寻找解决方案的人来说,这对我有用:
For anyone still looking for a solution, this worked for me: