简体   繁体   English

键绑定标签

[英]KeyBinding Label

I have a TreeView with the following definition: 我有一个具有以下定义的TreeView

<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. LeftDoubleClickInputBinding可以LeftDoubleClick工作。

But the InputBinding for the 'Delete'-Key doesn't work. 但是'Delete'键的InputBinding不起作用。

The Command where the KeyBinding is bound to looks like: CommandKeyBinding绑定到的样子:

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: 和DeleteFolder-Method看起来像:

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处理KeyBinding管理它

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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