c# - WPF - 菜单项缺少图标/图像

标签 c# wpf visual-studio-2010 icons menuitem

我得到的菜单项图标只出现在最后一个菜单项上。 如果我窥探应用程序只有最后一个菜单项在图标中有图像,而如果我调试所有菜单项似乎在图标中有图像。另外,如果我添加 submenuItem,一旦我打开子菜单,menuItem 上的图标就会消失,而最后一个子菜单会获得图标……有什么想法吗? PS:菜单项上的工具提示也不起作用。 我正在使用 Caliburn Micro 和 Fluent 色带控件。

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

最佳答案

您正在将 Icon 属性设置为 Style 中的 Image 控件。现在,只创建了一个 Style 副本,因此也只创建了一个 Image 副本。现在,任何控件一次只能有一个父控件。因此,当它分配给最后一个 MenuItem 时,它会从之前的 MenuItem 控件中删除。要解决此问题,请使用 Templates

不是设置 Header 属性,而是设置 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>

我不确定您正在使用的控制工具包公开了哪些属性。但是,我确定它们必须具有模板属性。

完成此操作后,您无需在样式中设置Icon 属性。

关于c# - WPF - 菜单项缺少图标/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4398954/

相关文章:

c# - MVVM - 在 View 中显示来自多个模型实体的消息

c# - AutoMapper - 如何映射到三层深度

c# - 如何在 ASP MVC 中选中复选框时禁用 DropDownList

c++ - TerminateProcess() 不会关闭应用程序

c++ - 在 Visual C++ 中使用时 curl 错误

c# - 如何仅在代码中创建 AxHost [C#]

C#继承问题

c# - 我如何知道 ListBoxItem 是否是 Wpf 的 ListBox 中的最后一项?

c# - 数据绑定(bind) ComboBox 中的本地化无法正常工作

WPF:使用模型 View View 模型刷新具有绑定(bind)属性的控件