Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. <TreeView x:Name="tr" ItemsSource="{Binding Root}">
  2. <TreeView.ItemContainerStyle>
  3. <Style TargetType="{x:Type TreeViewItem}">
  4. <Setter Property="local:AttachedTVIBinding.InputBindings">
  5. <Setter.Value>
  6. <InputBindingCollection>
  7. <KeyBinding Key="A" Command="{Binding SomeCommand}"/>
  8. <MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
  9. </InputBindingCollection>
  10. </Setter.Value>
  11. </Setter>
  12. </Style>
  13. </TreeView.ItemContainerStyle>
  14. </TreeView>
  15.  
  16. public class ConfigurationNodeViewModel : INotifyPropertyChanged
  17. {
  18. private DelegateCommand _someCommand;
  19.  
  20. public DelegateCommand SomeCommand
  21. {
  22. get { return _editDesignCommand; }
  23. }
  24. }
  25.  
  26. public class AttachedTVIBinding : Freezable
  27. {
  28. public static readonly DependencyProperty InputBindingsProperty =
  29. DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(AttachedTVIBinding),
  30. new FrameworkPropertyMetadata(new InputBindingCollection(),
  31. (sender, e) =>
  32. {
  33. var element = sender as UIElement;
  34. if (element == null) return;
  35. element.InputBindings.Clear();
  36. element.InputBindings.AddRange((InputBindingCollection)e.NewValue);
  37. }));
  38.  
  39. public static InputBindingCollection GetInputBindings(UIElement element)
  40. {
  41. return (InputBindingCollection)element.GetValue(InputBindingsProperty);
  42. }
  43.  
  44. public static void SetInputBindings(UIElement element, InputBindingCollection inputBindings)
  45. {
  46. element.SetValue(InputBindingsProperty, inputBindings);
  47. }
  48.  
  49. protected override Freezable CreateInstanceCore()
  50. {
  51.  
  52. return new AttachedTVIBinding();
  53. }
  54.  
  55. }
  56.  
  57. <InputBindingCollection x:Key="myBindings" x:Shared="False">
  58. <KeyBinding Key="A" Command="{Binding SomeCommand}"/>
  59. <MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
  60. </InputBindingCollection>
  61.  
  62. <Style TargetType="{x:Type TreeViewItem}">
  63. <Setter Property="local:AttachedTVIBinding.InputBindings" Value="{DynamicResource myBindings}"/>
  64. </Style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement