【发布时间】:2012-03-30 14:03:07
【问题描述】:
我想在单独的面板中显示 myElement.ContextMenu 图标。 我正在尝试这样做:
<ItemsControl ItemsSource="{Binding ElementName=myElement, Path=ContextMenu.ItemsSource}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type MenuItem}">
<Image Source="{Binding Icon}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但它向我显示了 MenuItems 而不是图像的集合。如果没有任何 ViewModel 和 xxx.xaml.cs 文件中的操作,我该如何做到这一点。
【问题讨论】:
-
为 myElement 提供 XAML(什么是 ContextMenu.ItemsSource)
-
ContextMenu 是任何 FrameworkElement 的上下文菜单。它没有任何特殊代码。只是 MenuItems 的集合。
-
如果省略 DataType 属性会怎样?
-
如果您在 XAML 中设置 MenuItems,那么 ItemsSource 将为空,不是吗?也许你应该写 Path=ContextMenu.Items
-
如果将数据模板中的
Image控件替换为ContentControl和Content={Binding Icon},会发生什么情况?