【问题标题】:KeyBinding Label键绑定标签
【发布时间】:2014-09-25 08:03:12
【问题描述】:

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

此视图与其代码隐藏文件有关:

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

LeftDoubleClickInputBinding 工作正常。

但“删除”键的 InputBinding 不起作用。

KeyBinding 绑定的Command 如下所示:

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

在我定义的构造函数中:

DeleteFolderCommand = new RelayCommand(DeleteFolder);

DeleteFolder-Method 看起来像:

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

我做错了什么?

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

【问题讨论】:

  • 绑定乍一看还不错。 SelectFolderCommand 工作吗? DeleteFolderCommand 是否与 SelectFolderCommand 存在于同一 VM 中?
  • 是的,SelectFolderCommand 工作正常。并且DeleteFolderCommand 在同一个ViewModel
  • 此时您可以检查Delete 密钥是否未在任何地方使用。并且当您按下删除时,该项目处于焦点
  • 我认为这是问题所在,标签上的键绑定不起作用
  • 将标签包裹在内容控件周围,并使用内容控件上的输入绑定。

标签: c# wpf inputbinding


【解决方案1】:

我通过直接在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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2015-09-24
    • 1970-01-01
    • 2014-02-16
    • 2015-08-24
    • 2012-01-12
    相关资源
    最近更新 更多