Search code examples
c#wpfinputbinding

KeyBinding Label


I have a TreeView with the following definition:

<TreeView ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}" x:Name="tree">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}">
            <Label Content="{Binding Name}" >
                <Label.InputBindings>
                    <KeyBinding Key="Delete"
                                Command="{Binding DataContext.DeleteFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
                    <MouseBinding MouseAction="LeftDoubleClick"
                                  Command="{Binding DataContext.SelectFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                                  CommandParameter="{Binding ElementName=tree, Path=SelectedItem}" />
                </Label.InputBindings>
            </Label>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

This view is boud to it's Code-Behind-File with:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

The InputBinding for the LeftDoubleClick just works fine.

But the InputBinding for the 'Delete'-Key doesn't work.

The Command where the KeyBinding is bound to looks like:

public ICommand DeleteFolderCommand
{
    get { return _deleteFolderCommand; }
    set
    {
        _deleteFolderCommand = value;
        OnPropertyChanged();
    }
}

and in the constructor I define:

DeleteFolderCommand = new RelayCommand(DeleteFolder);

and the DeleteFolder-Method just looks like:

private void DeleteFolder(object parameter)
{
  // Break-Point here will not be reached                
}

What am I doing wrong?

I've already checked the Output-Window for Binding-Errors, but there are none.


Solution

  • I managed it by handling the KeyBinding directly at the TreeView

    <TreeView ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}" x:Name="tree">
        <TreeView.InputBindings>
            <KeyBinding Key="Delete" Command="{Binding DataContext.DeleteFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
        </TreeView.InputBindings>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}">
                <Label Content="{Binding Name}">
                    <Label.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick"
                                    Command="{Binding DataContext.SelectFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                                    CommandParameter="{Binding ElementName=tree, Path=SelectedItem}" />
                    </Label.InputBindings>
                </Label>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>