Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <TreeView x:Name="tr" ItemsSource="{Binding Root}">
- <TreeView.ItemContainerStyle>
- <Style TargetType="{x:Type TreeViewItem}">
- <Setter Property="local:AttachedTVIBinding.InputBindings">
- <Setter.Value>
- <InputBindingCollection>
- <KeyBinding Key="A" Command="{Binding SomeCommand}"/>
- <MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
- </InputBindingCollection>
- </Setter.Value>
- </Setter>
- </Style>
- </TreeView.ItemContainerStyle>
- </TreeView>
- public class ConfigurationNodeViewModel : INotifyPropertyChanged
- {
- private DelegateCommand _someCommand;
- public DelegateCommand SomeCommand
- {
- get { return _editDesignCommand; }
- }
- }
- public class AttachedTVIBinding : Freezable
- {
- public static readonly DependencyProperty InputBindingsProperty =
- DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(AttachedTVIBinding),
- 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);
- }
- protected override Freezable CreateInstanceCore()
- {
- return new AttachedTVIBinding();
- }
- }
- <InputBindingCollection x:Key="myBindings" x:Shared="False">
- <KeyBinding Key="A" Command="{Binding SomeCommand}"/>
- <MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
- </InputBindingCollection>
- <Style TargetType="{x:Type TreeViewItem}">
- <Setter Property="local:AttachedTVIBinding.InputBindings" Value="{DynamicResource myBindings}"/>
- </Style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement