我有一个在项目上具有以下样式的菜单:
<Style TargetType="MenuItem">
<Setter Property="Template" Value="{StaticResource MenuItem}"/>
<Setter Property="Icon">
<Setter.Value>
<TextBlock FontWeight="Bold">Ic</TextBlock>
</Setter.Value>
</Setter>
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="ItemsSource" Value="{Binding SubItems}"/>
</Style>
( Name
和 SubItems
是类 MenuItem
的属性。
ItemsSource
菜单的属性绑定(bind)到 List<MenuItem>
类型的对象)
问题是“图标”部分只出现在菜单的最后一项上:
我最终想将每个项目绑定(bind)到它自己的图标,但这似乎也不起作用。 任何想法是什么导致了这种不当行为以及如何解决它?
更新 我见过这个:MenuItem style with icon creates only one icon
但它对我不起作用,因为 a. x:Shared=false
做了一个XamlParseException
和 b。如果我把它移出 Style.Resources
它没有异常(exception),但根本不起作用。请注意,我确实需要在 Style 内部使用它,因为最终我希望它绑定(bind)到我将 MenuItem 绑定(bind)到的类的属性。
最佳答案
不要共享 TextBlock
<TextBlock x:Key="tb" x:Shared="false" FontWeight="Bold">Ic</TextBlock>
<Style TargetType="{x:Type MenuItem}">
...
<Setter Property="Icon" Value="{StaticResource tb}"/>
</Style>
关于wpf - MenuItem.Icon 仅出现在最后一项上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058405/