如何在 silverlight 中将菜单项绑定到 ContextMenu,包括图标

发布于 2024-09-29 20:41:18 字数 1427 浏览 5 评论 0原文

我有一个上下文菜单,我希望通过绑定填充其菜单项。 下面的代码可以工作,

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤还要旧人安 2024-10-06 20:41:18

我创建一个上下文菜单以编程方式并设置内容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.

掀纱窥君容 2024-10-06 20:41:18

尝试使用此开源菜单:

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.

青芜 2024-10-06 20:41:18

我也有同样的问题,虽然我没有找到我想要的答案,但我找到了一个很好地满足我的需求的解决方法。我想你可能会喜欢看它,即使是在事后这么久。

看起来,如果 ContextMenu 类的 ItemsSource 包含 MenuItem 对象,则图标会很好地落在它所属的位置。我和你一样,正在使用视图模型,并且没有将 UI 逻辑注入到我的视图模型类中。

相反,我所做的是实现一个值转换器,将我的视图模型转换为 MenuItem 对象。它看起来有点像这样:


XAML:

<UserControl.Resources>
    <local:DelegatedValueConverter
        x:Key="LocalContextItemConverter"
        Converting="OnBindingContextMenu" />
</UserControl.Resources>

...

<toolkit:ContextMenu
    ItemsSource="{Binding ContextMenuItems, Converter={StaticResource LocalContextItemConverter}}" />

C# 代码隐藏

private void OnBindingContextMenu(object sender, ValueConvertingEventArgs e)
{
    var dataitems = e.Value as IEnumerable< NavigationItemViewModel >;
    if (dataitems != null)
    {
        var items = dataitems.Select(data => new MenuItem()
        {
            Header = data.Title,
            Icon = data.Icon,
            Tag = data
        }).ToList();
        var handler = new RoutedEventHandler(this.OnContextMenuClick);
        items.ForEach(item => item.Click += handler);
        e.Result = items
    }
}

private void OnContextMenuClick(object sender, RoutedEventArgs e)
{
}

讨论

此处详细描述,但这并不重要。您可以推出自己的值转换器并使用它。

我的视图模型的 Title 属性是一个字符串,IconImage 的实例。我使用菜单项的 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 the ContextMenu class contains MenuItem 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:

<UserControl.Resources>
    <local:DelegatedValueConverter
        x:Key="LocalContextItemConverter"
        Converting="OnBindingContextMenu" />
</UserControl.Resources>

...

<toolkit:ContextMenu
    ItemsSource="{Binding ContextMenuItems, Converter={StaticResource LocalContextItemConverter}}" />

C# Code-Behind

private void OnBindingContextMenu(object sender, ValueConvertingEventArgs e)
{
    var dataitems = e.Value as IEnumerable< NavigationItemViewModel >;
    if (dataitems != null)
    {
        var items = dataitems.Select(data => new MenuItem()
        {
            Header = data.Title,
            Icon = data.Icon,
            Tag = data
        }).ToList();
        var handler = new RoutedEventHandler(this.OnContextMenuClick);
        items.ForEach(item => item.Click += handler);
        e.Result = items
    }
}

private void OnContextMenuClick(object sender, RoutedEventArgs e)
{
}

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, and Icon is an instance of Image. I use the Tag 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文