在 ItemsControl.DataTemplate 中为 ContextMenu 设置 DataContext [英] Setting DataContext for ContextMenu in ItemsControl.DataTemplate

查看:20
本文介绍了在 ItemsControl.DataTemplate 中为 ContextMenu 设置 DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,用于 ItemsControl 的数据模板中.我想在每个项目上放置一个 ContextMenu 并让它调用 UserControl 的视图模型来处理命令.使用下面的 XAML,我可以在自定义控件上获取单击事件以调用用户控件视图模型中的 SelectedItemCommand.但是,对上下文菜单使用类似的语法失败.默认情况下,我获得每个自定义控件的视图模式.我使用的任何 RelativeSource 语法值都不会解析为用户控件的视图模型 (RelativeSource Self).

I have custom control that is being used in data template for an ItemsControl. I want to put a ContextMenu on each item and have it call the UserControl's View Model to process the command. Using the XAML below, I can get click events on the custom control to call the SelectedItemCommand in the user control view model. But, using similar syntax for the context menu fails. By default I get the view mode per each custom control. Any of the RelativeSource syntax value I use don't resolve to the user control's view model (RelativeSource Self).

魔法代码是什么?

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <controls:MyCustomItem Width="Auto"
                                                 Command="{Binding DataContext.SelectedItemCommand,
                                                                   RelativeSource={RelativeSource FindAncestor,
                                                                                                  AncestorType={x:Type ItemsControl}}}"
                                                 CommandParameter="{Binding}">
                    <controls:MyCustomItem.ContextMenu>
                        <ContextMenu>
                            <MenuItem Command="{Binding DataContext.ClearAlarmsCommand,
                                                        RelativeSource={RelativeSource FindAncestor,
                                                                                       AncestorType={x:Type ItemsControl}}}"
                                      Header="Clear All" />
                        </ContextMenu>
                    </controls:MyCustomItem.ContextMenu>
                </controls:MyCustomItem>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

推荐答案

ContextMenu 与 ItemsControl 不在同一可视化树中.因此,RelativeSource 和 ElementName 在 Binding 中不起作用,因为它们遍历 Visual 树以找到源.

ContextMenu doesn't lie in same Visual Tree as that of ItemsControl. Hence RelativeSource and ElementName won't work in Binding because they traverse Visual tree to found the source.

如果您使用的是 WPF 4.0 或更高版本,则可以使用 x:Reference 标记扩展与 ItemsControl dataContext 绑定.​​

If you are using WPF 4.0 or higher you can use x:Reference markup extension to bind with ItemsControl dataContext.

在 ItemsControl 上设置 x:Name 并使用 x:Reference 进行绑定,如下所示:

Set x:Name on ItemsControl and bind using x:Reference like this:

<ItemsControl x:Name="itemsControl">
   ....
   <MenuItem Command="{Binding DataContext.ClearAlarmsCommand,
                               Source={x:Reference itemsControl}}"
             Header="Clear All" />
   ....
</ItemControl>

<小时>

如果您的目标版本低于 WPF 4.0,您也可以使用 Freezable BindingProxy 方法.有关方法,请参阅我在此处上的回答.

这篇关于在 ItemsControl.DataTemplate 中为 ContextMenu 设置 DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆