简体   繁体   English

在 ContextMenu 中使用 ItemSource 进行控制

[英]Using ItemSource for control in ContextMenu

I was trying to make universal but dynamic control for my context menus accross whole application.我试图对整个应用程序中的上下文菜单进行通用但动态的控制。 To do that i move ContextMenu to Window.Resources and tried to use as before before i move it.为此,我将 ContextMenu 移动到 Window.Resources 并尝试在移动它之前像以前一样使用。 But this is not enough.但这还不够。 With my current implementation i cannot use xaml to use diffrent ItemSources for diffrent ContextMenus without making another contextmenu template.在我当前的实现中,我不能使用 xaml 将不同的 ItemSources 用于不同的 ContextMenus 而不制作另一个 contextmenu 模板。

So one more time in short: Can i use ItemSource in ListView still maintainging template connection.再简短一次:我可以在 ListView 中使用 ItemSource 仍然保持模板连接吗? Within Xaml.在 Xaml 中。 I know this would possible in c# code.我知道这在 c# 代码中是可能的。

<Window.Resources>
    <ContextMenu x:Key="ReadOnlyContextMenuTemplate" ItemsSource="{Binding Menu.MenuCommands}">
        <ContextMenu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Header" Value="{Binding Text}"/>
                <Setter Property="Command" Value="{Binding Self}"/>
                <Setter Property="InputGestureText" Value="{Binding GestureText}"/>
                <Setter Property="Icon" Value="{Binding IconImage}">
                </Setter>
            </Style>
        </ContextMenu.ItemContainerStyle>
    </ContextMenu>
</Window.Resources>

Usage用法

<ListView ContextMenu="{StaticResource ReadOnlyContextMenuTemplate}"/>

MenuCommands class菜单命令类

    public class Menu
    {
        private List<ICommand> _menuCommands;
        public List<ICommand> MenuCommands
        {
            get { return _menuCommands; }
        }

        private ICommand _commandCopy;
        public ICommand CommandCopy
        {
            get
            {
                return _commandCopy ?? (_commandCopy = new TextIconCommandHandler(
                    (parameter) => Copy(parameter), true, "Copy", "Copy.png", Key.C, ModifierKeys.Control));
            }
        }

        private ICommand _commandSelect;
        public ICommand CommandSelect
        {
            get
            {
                return _commandSelect ?? (_commandSelect = new TextIconCommandHandler(
                    (parameter) => Select(parameter), true, "Select all", "Select.png", Key.A, ModifierKeys.Control));
            }
        }

        private ICommand _commandSave;
        public ICommand CommandSave
        {
            get
            {
                return _commandSave ?? (_commandSave = new TextIconCommandHandler(
                    (parameter) => Save(parameter), true, "Save", "Save.png", Key.S, ModifierKeys.Control));
            }
        }

        public Menu()
        {
            _menuCommands = new List<ICommand>();
            var properties = this.GetType().GetProperties();
            foreach (var item in properties)
            {
                var command = item.GetValue(this, null) as ICommand;
                if(command != null)
                {
                    _menuCommands.Add(command);
                }
            }
        }
     }

TextIconCommandHandlerClass (sorry for wall of text) TextIconCommandHandlerClass(对不起,文字墙)

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace TestingTool.Commands
{
    public class CommandHandler : ICommand
    {
        private Action<object> _action;
        private bool _canExecute;
        private KeyGesture _keyGesture;
        private ICommand _self;

        public ICommand Self
        {
            get { return _self; }
            set { _self = value; }
        }

        public KeyGesture KeyGesture
        {
            get { return _keyGesture; }
            set { _keyGesture = value; }
        }

        public string GestureText
        {
            get { return _keyGesture.GetDisplayStringForCulture(CultureInfo.CurrentUICulture); }
        }

        public CommandHandler(Action<object> action, bool canExecute, Key key = Key.None, ModifierKeys keysModifier = ModifierKeys.None)
        {
            _self = this;
            _action = action;
            _canExecute = canExecute;
            _keyGesture = new KeyGesture(key, keysModifier);       
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _action(parameter);
        }
    }

    public class TextCommandHandler : CommandHandler
    {
        private string _text;

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public TextCommandHandler(Action<object> action, bool canExecute, string text, Key key = Key.None, ModifierKeys keysModifier = ModifierKeys.None)
            : base(action, canExecute, key, keysModifier)
        {
            _text = text;
        }
    }

    public class TextIconCommandHandler : TextCommandHandler
    {
        private string _iconName;
        private Image _iconImage;

        public Image IconImage
        {
            get { return _iconImage; }
            set { _iconImage = value; }
        } 

        public TextIconCommandHandler(Action<object> action, bool canExecute, string text, string iconName, Key key = Key.None, ModifierKeys keysModifier = ModifierKeys.None)
            : base(action, canExecute, text, key, keysModifier)
        {
            _iconName = "Resources/" + iconName;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new Uri(_iconName, UriKind.Relative);
            bi.EndInit();
            _iconImage = new Image();
            _iconImage.Source = bi;
        }
    }
}

为您的 ViewModel 属性使用标准名称,该名称将用作各种控件的 ContextMenu 的 ItemsSource。

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

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