如何在 silverlight 中将菜单项绑定到 ContextMenu,包括图标
我有一个上下文菜单,我希望通过绑定填充其菜单项。 下面的代码可以工作,
<Button>
<controlsInputToolkit:ContextMenuService.ContextMenu>
<controlsInputToolkit:ContextMenu ItemsSource="{Binding MenuItems}">
<controlsInputToolkit:ContextMenu.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</controlsInputToolkit:ContextMenu.ItemTemplate>
</controlsInputToolkit:ContextMenu>
</controlsInputToolkit:ContextMenuService.ContextMenu>
</Button>
但有一个问题,ItemTemplate 代表菜单项的文本区域,据我所知不允许您设置图标,
所以我尝试通过 ItemContainerStyle 进行绑定,如下例所示,
<Button>
<controlsInputToolkit:ContextMenuService.ContextMenu>
<controlsInputToolkit:ContextMenu ItemsSource="{Binding MenuItems}">
<controlsInputToolkit:ContextMenu.ItemContainerStyle>
<Style TargetType="controlsInputToolkit:MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</controlsInputToolkit:ContextMenu.ItemContainerStyle>
</controlsInputToolkit:ContextMenu>
</controlsInputToolkit:ContextMenuService.ContextMenu>
</Button>
但 silverlight 不这样 做似乎喜欢这样,
有什么想法吗?
I have a context menu and i want its menu items populated via a binding.
The following code works
<Button>
<controlsInputToolkit:ContextMenuService.ContextMenu>
<controlsInputToolkit:ContextMenu ItemsSource="{Binding MenuItems}">
<controlsInputToolkit:ContextMenu.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</controlsInputToolkit:ContextMenu.ItemTemplate>
</controlsInputToolkit:ContextMenu>
</controlsInputToolkit:ContextMenuService.ContextMenu>
</Button>
but there's one problem, the ItemTemplate represents the Text area of a menu item and so as far as im aware doesnt allow you to set the icon
so i tried doing the binding via the ItemContainerStyle as in the below example
<Button>
<controlsInputToolkit:ContextMenuService.ContextMenu>
<controlsInputToolkit:ContextMenu ItemsSource="{Binding MenuItems}">
<controlsInputToolkit:ContextMenu.ItemContainerStyle>
<Style TargetType="controlsInputToolkit:MenuItem">
<Setter Property="Header" Value="{Binding Name}"/>
</Style>
</controlsInputToolkit:ContextMenu.ItemContainerStyle>
</controlsInputToolkit:ContextMenu>
</controlsInputToolkit:ContextMenuService.ContextMenu>
</Button>
but silverlight doesnt seem to like that
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建一个上下文菜单以编程方式并设置内容MenuItem.Header 属性似乎适用于图像。有很多的示例设置文本的。
I create a context menu programmatically and setting the content to the MenuItem.Header property seems to work just fine for images. There are a lot of examples of setting text.
尝试使用此开源菜单:
http://sl4popupmenu.codeplex.com
该模板允许直接绑定图像。
希望这有帮助。
Try using this open source menu instead:
http://sl4popupmenu.codeplex.com
The template allows binding of images directly.
Hope this helps.
我也有同样的问题,虽然我没有找到我想要的答案,但我找到了一个很好地满足我的需求的解决方法。我想你可能会喜欢看它,即使是在事后这么久。
看起来,如果
ContextMenu
类的ItemsSource
包含MenuItem
对象,则图标会很好地落在它所属的位置。我和你一样,正在使用视图模型,并且没有将 UI 逻辑注入到我的视图模型类中。相反,我所做的是实现一个值转换器,将我的视图模型转换为
MenuItem
对象。它看起来有点像这样:XAML:
C# 代码隐藏
讨论
此处详细描述,但这并不重要。您可以推出自己的值转换器并使用它。
我的视图模型的
Title
属性是一个字符串,Icon
是Image
的实例。我使用菜单项的Tag
属性来保留对视图模型的引用,以便我可以在单击事件期间引用其中的非可视数据。这个解决方案很方便,因为它仍然在 UI 和视图模型之间提供了一些分离,尽管在这种情况下我更喜欢纯声明性的解决方案。
我希望这有帮助!
I've had the same question, and though I did not find the answer I wanted, I found a workaround that met my needs nicely. I thought you might like to see it, even this long after-the-fact.
It appears that if the
ItemsSource
of theContextMenu
class containsMenuItem
objects, then the icon lands nicely where it belongs. I, like you, was using a view model and didn't what to inject UI logic into my view model class.What I did instead was implement a value converter to transform my view model into
MenuItem
objects. It looked a little something like this:XAML:
C# Code-Behind
Discussion
The
DelegatedValueConverter
is a little something I describe in detail here, but it is not really important. You could roll your own value converter and use it instead.The
Title
property of my view model is a string, andIcon
is an instance ofImage
. I use theTag
property of the menu item to keep a reference to my view model so that I can refer to the non-visual data in it during a click event.This solution is convenient because it still provides some separation between the UI and the view model, though I would prefer in this case a purely-declarative solution.
I hope this helps!