WPF - 菜单项缺少图标/图像
我的菜单项图标仅出现在最后一个菜单项上。 如果我窥探应用程序,则只有最后一个菜单项在图标中具有图像,而如果我调试所有菜单项,则似乎在图标中具有图像。另外,如果我添加子菜单项,一旦我打开子菜单并且最后一个子菜单获得图标,菜单项上的图标就会消失...知道吗? PS:菜单项上的工具提示也不起作用。 我使用的是 Caliburn Micro 和 Fluent Ribbon 控件。
<ControlTemplate x:Key="dropDownButton">
<ef:DropDownButton Header="{Binding DisplayName}"
ItemsSource="{Binding Items}"
LargeIcon="{Binding LargeIconPath}"
cm:Message.Attach="ClickAction()"
ef:KeyTip.Keys="{Binding KeyTip}">
<ef:DropDownButton.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header"
Value="{Binding DisplayName}"/>
<Setter Property="Icon">
<Setter.Value>
<Image Source="{Binding Path=IconPath}"/>
</Setter.Value>
</Setter>
<Setter Property="ItemsSource"
Value="{Binding Items}"/>
<Setter Property="cm:Message.Attach"
Value="ClickAction()"/>
<Setter Property="ef:KeyTip.Keys"
Value="{Binding KeyTip}"/>
<Setter Property="ToolTip">
<Setter.Value>
<ef:ScreenTip Title="{Binding DisplayName}"
HelpTopic="ScreenTip help ..."
Image="{Binding LargeIconPath}"
Text="Text for ScreenTip"/>
</Setter.Value>
</Setter>
</Style>
</ef:DropDownButton.ItemContainerStyle>
<ef:DropDownButton.ToolTip>
<ef:ScreenTip Title="{Binding DisplayName}"
HelpTopic="ScreenTip help ..."
Image="{Binding LargeIconPath}"
Text="Text for ScreenTip"/>
</ef:DropDownButton.ToolTip>
</ef:DropDownButton>
Im getting menuItem icon appearing only on last menuItem.
If i snoop the app only last menuItem has image in icon, while if i debug all MenuItems appear to have image in icon. Also if i add submenuItem the icon on menuItem dissapears once i open submenus and the last submenu gets the icon... Any idea? PS: also tooltips on menu item dont work.
Im using caliburn micro and fluent ribbon controls.
<ControlTemplate x:Key="dropDownButton">
<ef:DropDownButton Header="{Binding DisplayName}"
ItemsSource="{Binding Items}"
LargeIcon="{Binding LargeIconPath}"
cm:Message.Attach="ClickAction()"
ef:KeyTip.Keys="{Binding KeyTip}">
<ef:DropDownButton.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header"
Value="{Binding DisplayName}"/>
<Setter Property="Icon">
<Setter.Value>
<Image Source="{Binding Path=IconPath}"/>
</Setter.Value>
</Setter>
<Setter Property="ItemsSource"
Value="{Binding Items}"/>
<Setter Property="cm:Message.Attach"
Value="ClickAction()"/>
<Setter Property="ef:KeyTip.Keys"
Value="{Binding KeyTip}"/>
<Setter Property="ToolTip">
<Setter.Value>
<ef:ScreenTip Title="{Binding DisplayName}"
HelpTopic="ScreenTip help ..."
Image="{Binding LargeIconPath}"
Text="Text for ScreenTip"/>
</Setter.Value>
</Setter>
</Style>
</ef:DropDownButton.ItemContainerStyle>
<ef:DropDownButton.ToolTip>
<ef:ScreenTip Title="{Binding DisplayName}"
HelpTopic="ScreenTip help ..."
Image="{Binding LargeIconPath}"
Text="Text for ScreenTip"/>
</ef:DropDownButton.ToolTip>
</ef:DropDownButton>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在将
Icon
属性设置为Style
中的Image
控件。现在,仅创建了Style
的一份副本,因此也仅创建了Image
的一份副本。现在,任何控件一次只能有一个父控件。因此,当它被分配给最后一个MenuItem
时,它就会从之前的MenuItem
控件中删除。要解决此问题,请使用模板
。不要设置
Header
属性,而是设置HeaderTemplate
:我不确定您正在使用的控件工具包公开了哪些属性。但是,我确信他们必须有一个模板属性。
完成此操作后,您不需要在样式中设置
Icon
属性。You are setting
Icon
property to anImage
control inStyle
. Now, only one copy ofStyle
is created and thus, only one copy ofImage
is created. Now, any control can have only one parent at a time. So, when it is assigned to lastMenuItem
, it is removed from previousMenuItem
controls. To fix this, useTemplates
.Instead of setting
Header
property, setHeaderTemplate
:I'm not sure of what properties are exposed by the control toolkit you are using. But, I'm sure they must have a template property.
After doing this, you don't need to set
Icon
property in style.我成功地在 ResourceDictionary 中使用了以下条目:
I successfully use the following entries in a ResourceDictionary:
工作原理如下:
Works like this:
由于某种原因,当 Image 是 x:Shared = false 的静态资源时的方法对我不起作用。仅最后一个菜单项显示图标。我尝试过静态资源和动态资源。这是我的解决方案:
示例:
我认为它比使用资源更具可读性,并且您不需要更改 MenuItem 的 HeaderTemplate。您还可以为 ImageSource 或 Image 实现一些缓存机制。
For some reason approach when Image is static resource with x:Shared = false doesn't work for me. Only last menu item shows icon. I've tried both StaticResource and DynamicResource. Here is my solution:
Sample:
I consider it to be more readable than using resource and you don't need to change MenuItem's HeaderTemplate. You can also implement some caching mechanism for ImageSource or Image.
1.将现有文件...图像文件添加到资源中(如果您已经有一个,请跳过它)。
2. 在解决方案资源管理器中选择此图像文件。
3. 将构建操作更改为资源。
最后,您可以通过简单的调用将此图像添加到 XAML:
结果:
1. Add Existing File... image file to resources (if you already have one, skip it).
2. In Solution Explorer select this image file.
3. Change Build Action to Resource.
And finally, you can add this image to XAML with simple call:
The Result: