我正在尝试使用带有KeyBinding的TreeViewItem和MenuContext来执行位于我的viewmodel上的命令.
目前,使用上下文菜单,在正确的viewmodel实例上调用该命令.
但是,当我选择TreeViewItem并按“C”键时,将在“根”viewmodel上调用该命令.
我也尝试过扩展KeyBinding类(Keybinding a RelayCommand)而没有运气.
也许我会走错路:我只想显示正确的MessageBox,如果我使用上下文菜单或键.
名为WpfTest的WPF项目的代码示例.
MainWindow.xaml
<Window x:Class="WpfTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:WpfTest" Title="MainWindow" Height="350" Width="525"> <Grid> <TreeView ItemsSource="{Binding}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Child}" DataType="{x:Type vm:viewmodel}"> <TextBlock Text="{Binding Name}" /> </HierarchicalDataTemplate> </TreeView.ItemTemplate> <TreeView.ItemContainerStyle> <Style targettype="{x:Type TreeViewItem}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="{Binding Name}" Command="{Binding SomeCommand}" CommandParameter="{Binding}"/> </ContextMenu> </Setter.Value> </Setter> <Setter Property="vm:MyAttached.InputBindings"> <Setter.Value> <InputBindingCollection> <KeyBinding Key="C" Command="{Binding SomeCommand}" CommandParameter="{Binding}"/> </InputBindingCollection> </Setter.Value> </Setter> </Style> </TreeView.ItemContainerStyle> </TreeView> </Grid> </Window>
MainWindow.xaml.cs:
namespace WpfTest { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Input; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new List<viewmodel> { new viewmodel { Name = "Parent",Child = new ObservableCollection<viewmodel> { new viewmodel { Name = "Child 1" },new viewmodel { Name = "Child 2" },new viewmodel { Name = "Child 3" } } } }; } } public class viewmodel { public string Name { get; set; } public ObservableCollection<viewmodel> Child { get; set; } public ICommand SomeCommand { get; set; } public viewmodel() { this.someCommand = new RelayCommand<viewmodel>(OnCommandExecuted); } private void OnCommandExecuted(viewmodel parameter) { MessageBox.Show("CommandExecuted on " + Name + " with parameter " + parameter.Name); } } public class MyAttached { public static readonly DependencyProperty InputBindingsProperty = DependencyProperty.Registerattached("InputBindings",typeof(InputBindingCollection),typeof(MyAttached),new FrameworkPropertyMetadata(new InputBindingCollection(),(sender,e) => { var element = sender as UIElement; if (element == null) return; element.InputBindings.Clear(); element.InputBindings.AddRange((InputBindingCollection)e.NewValue); })); public static InputBindingCollection GetInputBindings(UIElement element) { return (InputBindingCollection)element.GetValue(InputBindingsProperty); } public static void SetInputBindings(UIElement element,InputBindingCollection inputBindings) { element.SetValue(InputBindingsProperty,inputBindings); } } public class RelayCommand<T> : ICommand { readonly Action<T> _execute = null; public RelayCommand(Action<T> execute) { _execute = execute; } public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { _execute((T)parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } } }
解决方法
问题是:Style只为所有ListViewItem创建一个InputBindingCollection,因此您必须非常小心Setter.Values.
这是修复:
<TreeView ItemsSource="{Binding}"> <TreeView.Resources> <!-- x:Shared="False" forces the new creation of that object whenever referenced --> <InputBindingCollection x:Shared="False" x:Key="InputBindings"> <KeyBinding Key="C" Command="{Binding SomeCommand}" CommandParameter="{Binding}" /> </InputBindingCollection> </TreeView.Resources> <!-- ... --> <TreeView.ItemContainerStyle> <Style targettype="{x:Type TreeViewItem}"> <!-- ... --> <Setter Property="vm:MyAttached.InputBindings" Value="{StaticResource InputBindings}"/> </Style> </TreeView.ItemContainerStyle> </TreeView>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。