简体   繁体   English

您可以将项目添加到父菜单中的上下文菜单吗?

[英]Can you add items to a context menu in a parents menu?

or conversly can you import a parents menu items into a child window?或者相反,您可以将父菜单项导入子窗口吗?

So I have an UI that is composed of controls that all have their own context menu, but we also have a datagrid that get's nested inside our generic contaier.所以我有一个由控件组成的 UI,这些控件都有自己的上下文菜单,但我们还有一个数据网格,它嵌套在我们的通用容器中。

So when you right click the datagrid elements I want to show both the context items I've created for the datagrid and the items from the generic container.因此,当您右键单击数据网格元素时,我想同时显示我为数据网格创建的上下文项和通用容器中的项。

Here is a solution using a few Attached Properties to manually combine multiple ContextMenus together.这是使用几个附加属性手动将多个 ContextMenu 组合在一起的解决方案。

Note: Since it is using XAMLReader to clone the MenuItems, this solution won't preserve bindings in the inherited MenuItems.注意:由于它使用 XAMLReader 来克隆 MenuItem,因此此解决方案不会保留继承的 MenuItem 中的绑定。

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml;

namespace ContextMenuSample
{
    public static class InheritMenu
    {
        public static readonly DependencyProperty ContextMenuProperty = DependencyProperty.RegisterAttached(
            "ContextMenu", typeof(ContextMenu), typeof(InheritMenu), new FrameworkPropertyMetadata(null, OnContextMenuChanged));
        private static void OnContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SetContextMenu(d as FrameworkElement);
        }
        public static void SetContextMenu(FrameworkElement element, ContextMenu value)
        {
            element.SetValue(ContextMenuProperty, value);
        }
        public static ContextMenu GetContextMenu(FrameworkElement element)
        {
            return (ContextMenu)element.GetValue(ContextMenuProperty);
        }

        public static readonly DependencyProperty ParentMenuProperty = DependencyProperty.RegisterAttached(
            "ParentMenu", typeof(ContextMenu), typeof(InheritMenu), new FrameworkPropertyMetadata(null, OnParentMenuChanged));
        private static void OnParentMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SetContextMenu(d as FrameworkElement);
        }
        public static void SetParentMenu(FrameworkElement element, ContextMenu value)
        {
            element.SetValue(ParentMenuProperty, value);
        }
        public static ContextMenu GetParentMenu(FrameworkElement element)
        {
            return (ContextMenu)element.GetValue(ParentMenuProperty);
        }

        private static void SetContextMenu(FrameworkElement element)
        {
            var context = GetContextMenu(element);
            var parent = GetParentMenu(element);
            if (context == null || parent == null) return;
            var menu = new ContextMenu();
            foreach (var item in parent.Items)
                menu.Items.Add(SimpleXamlClone(item));
            menu.Items.Add(new Separator());
            foreach (var item in context.Items)
                menu.Items.Add(SimpleXamlClone(item));
            element.ContextMenu = menu;
        }

        public static object SimpleXamlClone(object original)
        {
            var xaml = XamlWriter.Save(original);
            var reader = new StringReader(xaml);
            var xml = XmlReader.Create(reader);
            return XamlReader.Load(xml);
        }
    }
}

<Window x:Class="ContextMenuSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:ContextMenuSample"
    Title="Window1" Height="250" Width="500">
    <Window.Resources>
        <l:MyCommand x:Key="MainCommand" Message="Main Command!" />
        <l:MyCommand x:Key="ScopeACommand" Message="Scope A Command!" />
        <l:MyCommand x:Key="ScopeBCommand" Message="Scope B Command!" />
        <l:MyCommand x:Key="ScopeCCommand" Message="Scope C Command!" />
        <l:MyCommand x:Key="ScopeDCommand" Message="Scope D Command!" />
        <Style TargetType="Label">
            <Setter Property="HorizontalAlignment" Value="Center" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Center" />
        </Style>
    </Window.Resources>
    <!-- Main Scope -->
    <Grid Margin="8" Background="LightGray">
        <Grid.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Main Scope" Command="{StaticResource MainCommand}" />
            </ContextMenu>
        </Grid.ContextMenu>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Label Grid.ColumnSpan="2" Grid.Row="0">Main Scope</Label>
        <Label Grid.ColumnSpan="2" Grid.Row="1">Check ContextMenu Here!</Label>

        <!-- Scope A -->
        <StackPanel Grid.Column="0" Grid.Row="2" Margin="8" Background="LightBlue">
            <l:InheritMenu.ParentMenu>
                <Binding Path="ContextMenu" RelativeSource="{RelativeSource AncestorType={x:Type Grid}}" />
            </l:InheritMenu.ParentMenu>
            <l:InheritMenu.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Scope A" Command="{StaticResource ScopeACommand}" />
                </ContextMenu>
            </l:InheritMenu.ContextMenu>
            <Label>Scope A</Label>
            <Label>Check ContextMenu Here!</Label>

            <!-- Scope C -->
            <StackPanel Margin="8" Background="LightGreen">
                <l:InheritMenu.ParentMenu>
                    <Binding Path="ContextMenu" RelativeSource="{RelativeSource AncestorType={x:Type StackPanel}}" />
                </l:InheritMenu.ParentMenu>
                <l:InheritMenu.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Scope C" Command="{StaticResource ScopeCCommand}" />
                    </ContextMenu>
                </l:InheritMenu.ContextMenu>
                <Label>Scope C</Label>
                <Label>Check ContextMenu Here!</Label>
            </StackPanel>
        </StackPanel>

        <!-- Scope B -->
        <StackPanel Grid.Column="1" Grid.Row="2" Margin="8" Background="LightCoral">
            <l:InheritMenu.ParentMenu>
                <Binding Path="ContextMenu" RelativeSource="{RelativeSource AncestorType={x:Type Grid}}" />
            </l:InheritMenu.ParentMenu>
            <l:InheritMenu.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Scope B" Command="{StaticResource ScopeBCommand}" />
                </ContextMenu>
            </l:InheritMenu.ContextMenu>
            <Label>Scope B</Label>
            <Label>Check ContextMenu Here!</Label>

            <!-- Scope D -->
            <StackPanel Margin="8" Background="LightSalmon">
                <l:InheritMenu.ParentMenu>
                    <Binding Path="ContextMenu" RelativeSource="{RelativeSource AncestorType={x:Type StackPanel}}" />
                </l:InheritMenu.ParentMenu>
                <l:InheritMenu.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Scope D" Command="{StaticResource ScopeDCommand}" />
                    </ContextMenu>
                </l:InheritMenu.ContextMenu>
                <Label>Scope D</Label>
                <Label>Check ContextMenu Here!</Label>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

public class MyCommand : ICommand
{
    public string Message { get; set; }
    public void Execute(object parameter) { MessageBox.Show(Message); }
    public bool CanExecute(object parameter) { return true; }
    public event EventHandler CanExecuteChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM