WPF - 菜单项缺少的图标/图片 [英] WPF - MenuItem missing Icon/Image

查看:281
本文介绍了WPF - 菜单项缺少的图标/图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时得到仅去年菜单项出现的菜单项的图标。
。如果我窥探应用程序只剩下最后菜单项在图标图像,而如果我调试所有的MenuItems似乎有形象的图标。另外,如果我加上菜单项自败submenuItem的图标一次我打开子菜单和子菜单最后得到的图标......你知道吗? PS:也对工具提示的菜单项不工作。 。使用卡利微流畅区控件

 <控件模板X:键=dropDownButton> 
< EF:DropDownButton标题={结合显示名称}
的ItemsSource ={绑定表项}
LargeIcon ={结合LargeIconPath}
厘米:Message.Attach =ClickAction()
EF:KeyTip.Keys ={绑定快捷键提示}>
< EF:DropDownButton.ItemContainerStyle>
<风格的TargetType =菜单项>
< setter属性=标题
值={结合显示名称}/>
< setter属性=图标>
< Setter.Value>
<图像来源={绑定路径= IconPath}/>
< /Setter.Value>
< /二传手>
< setter属性=的ItemsSource
值={绑定表项}/>
< setter属性=厘米:Message.Attach
值=ClickAction()/>
< setter属性=EF:KeyTip.Keys
值={绑定快捷键提示}/>
< setter属性=工具提示>
< Setter.Value>
< EF:屏幕提示标题={结合显示名称}
HELPTOPIC =屏幕提示的帮助......
图像={结合LargeIconPath}
文本=TEXT对于屏幕提示/>
< /Setter.Value>
< /二传手>
< /样式和GT;
< / EF:DropDownButton.ItemContainerStyle>
< EF:DropDownButton.ToolTip>
< EF:屏幕提示标题={结合显示名称}
HELPTOPIC =屏幕提示的帮助......
图像={结合LargeIconPath}
文本=TEXT对于屏幕提示/>
< / EF:DropDownButton.ToolTip>
< / EF:DropDownButton>


解决方案

您正在设置图标<在属性设置为图片控制>风格。现在,只创建一个风格的副本,因此,创建图像只有一个副本。现在,任何控制可以在同一时间只能有一个父。所以,当它被分配给持续菜单项,它是从以前的菜单项控件中删除。为了解决这个问题,可以使用模板



而不是标题属性设置 HeaderTemplate中

 < setter属性= HeaderTemplate中> 
< Setter.Value>
<&DataTemplate的GT;
<网格和GT;
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =*/>
< /Grid.ColumnDefinitions>
<图像Grid.Column =0
来源={绑定路径= IconPath}/>
< TextBlock的Grid.Column =1
文本={结合显示名称}/>
< /网格和GT;
< / DataTemplate中>
< /Setter.Value>
< /二传手>



我不知道是什么属性由您正在使用的控件工具包暴露出来。但是,我敢肯定,他们必须有一个模板属性。



这样做后,就不需要设置图标的样式属性。


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>

解决方案

You are setting Icon property to an Image control in Style. Now, only one copy of Style is created and thus, only one copy of Image is created. Now, any control can have only one parent at a time. So, when it is assigned to last MenuItem, it is removed from previous MenuItem controls. To fix this, use Templates.

Instead of setting Header property, set HeaderTemplate:

            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="0"
                                   Source="{Binding Path=IconPath}" />
                            <TextBlock Grid.Column="1"
                                       Text="{Binding DisplayName}" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>

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.

这篇关于WPF - 菜单项缺少的图标/图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆