键绑定标签

时间:2014-09-25 08:03:13

标签: c# wpf inputbinding

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

这个视图对于它的Code-Behind-File而言是:

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

InputBinding的{​​{1}}工作正常。

但是&#39;删除&#39; -Key的LeftDoubleClick无效。

InputBinding绑定的Command

KeyBinding

并在我定义的构造函数中:

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

和DeleteFolder-Method看起来像:

DeleteFolderCommand = new RelayCommand(DeleteFolder);

我做错了什么?

我已经检查了输出窗口的绑定错误,但没有。

1 个答案:

答案 0 :(得分:0)

我通过直接在KeyBinding

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