我有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);
我做错了什么?
我已经检查了输出窗口的绑定错误,但没有。
答案 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>