简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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