TreeView 的 HierarchicalDataTemplate 中的 KeyBinding [英] KeyBinding in HierarchicalDataTemplate of TreeView

查看:31
本文介绍了TreeView 的 HierarchicalDataTemplate 中的 KeyBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TreeView.我想通过点击 F2 启用 EditLeafCommand.

I have a TreeView. I would like to enable EditLeafCommand by clicking F2.

型号:

public class Leaf
{
    public string LeafName { get; set; }
    public bool HasChildren { get; set; }        
}

窗口的视图模型:

public MainWindowViewModel{

  public ReadOnlyCollection<LeafViewModel> Leafs
  {
        get { return leafs; }
  }
}

TreeView 的 ViewModel:

public class LeafViewModel : TreeViewItemViewModel
{
    public ObservableCollection<TreeViewItemViewModel> Children
    {
        get { return _children; }
    }
    public string LeafName { get; set; }   
    public RelayCommand EditLeafCommand { get; set; }     

}

XAML:

<TreeView  ItemsSource="{Binding Leafs}">               
   <TreeView.InputBindings>
      <KeyBinding Key="F2" Command="{Binding SelectedItem.EditLeafCommand, 
                           diag:PresentationTraceSources.TraceLevel=High}"/>
   </TreeView.InputBindings>
   <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type vm:LeafViewModel}" 
                                     ItemsSource="{Binding Children}">
         <StackPanel Orientation="Horizontal">               
            <TextBox Text="{Binding LeafName}" IsReadOnly="{Binding IsReadOnlyItem}" 
            Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}">
               <TextBox.ContextMenu>
                  <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                     <MenuItem Command="{Binding EditLeafCommand}" CommandParameter="{Binding ALeaf}" Header="Edit" />
                  </ContextMenu>
               </TextBox.ContextMenu>
            </TextBox>
         </StackPanel>
      </HierarchicalDataTemplate>
   </TreeView.Resources>
</TreeView>

输出窗口:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=2683661) for Binding (hash=47044325)

System.Windows.Data Warning: 56 : Created BindingExpression (hash=2683661) for Binding (hash=47044325)

System.Windows.Data 警告:58:路径:'EditLeafCommand'

System.Windows.Data Warning: 58 : Path: 'EditLeafCommand'

System.Windows.Data 警告:60:BindingExpression (hash=2683661):默认模式解析为 OneWay

System.Windows.Data Warning: 60 : BindingExpression (hash=2683661): Default mode resolved to OneWay

System.Windows.Data 警告:61:BindingExpression (hash=2683661):默认更新触发器解析为 PropertyChanged

System.Windows.Data Warning: 61 : BindingExpression (hash=2683661): Default update trigger resolved to PropertyChanged

System.Windows.Data 警告:62:BindingExpression (hash=2683661):附加到System.Windows.Input.KeyBinding.Command (hash=29578451)

System.Windows.Data Warning: 62 : BindingExpression (hash=2683661): Attach to System.Windows.Input.KeyBinding.Command (hash=29578451)

System.Windows.Data 警告:64 : BindingExpression (hash=2683661):使用框架导师

System.Windows.Data Warning: 64 : BindingExpression (hash=2683661): Use Framework mentor

System.Windows.Data 警告:67:BindingExpression (hash=2683661):解析源

System.Windows.Data Warning: 67 : BindingExpression (hash=2683661): Resolving source

System.Windows.Data 警告:69:BindingExpression (hash=2683661):未找到框架导师

System.Windows.Data Warning: 69 : BindingExpression (hash=2683661): Framework mentor not found

System.Windows.Data 警告:65 : BindingExpression (hash=2683661):解析源延迟

System.Windows.Data Warning: 65 : BindingExpression (hash=2683661): Resolve source deferred

System.Windows.Data Warning: 95 : BindingExpression (hash=2683661): Got InheritanceContextChanged event from KeyBinding (hash=29578451)

System.Windows.Data Warning: 95 : BindingExpression (hash=2683661): Got InheritanceContextChanged event from KeyBinding (hash=29578451)

System.Windows.Data 警告:67:BindingExpression (hash=2683661):解析源

System.Windows.Data Warning: 67 : BindingExpression (hash=2683661): Resolving source

System.Windows.Data Warning: 70 : BindingExpression (hash=2683661): Found data context element: TreeView (hash=11903911) (OK)

System.Windows.Data Warning: 70 : BindingExpression (hash=2683661): Found data context element: TreeView (hash=11903911) (OK)

System.Windows.Data 警告:78:BindingExpression (hash=2683661):使用根项 MainWindowViewModel (hash=44115416) 激活

System.Windows.Data Warning: 78 : BindingExpression (hash=2683661): Activate with root item MainWindowViewModel (hash=44115416)

System.Windows.Data 警告:108:BindingExpression (hash=2683661):在级别 0 - 对于 MainWindowViewModel.EditLeafCommand 找到访问器

System.Windows.Data Warning: 108 : BindingExpression (hash=2683661): At level 0 - for MainWindowViewModel.EditLeafCommand found accessor

System.Windows.Data 错误:40:BindingExpression 路径错误:在对象"MainWindowViewModel"(HashCode=44115416)上找不到EditLeafCommand"属性.BindingExpression:Path=EditLeafCommand;DataItem='MainWindowViewModel' (HashCode=44115416);目标元素是KeyBinding"(HashCode=29578451);目标属性是'Command'(类型'ICommand')

System.Windows.Data Error: 40 : BindingExpression path error: 'EditLeafCommand' property not found on 'object' ''MainWindowViewModel' (HashCode=44115416)'. BindingExpression:Path=EditLeafCommand; DataItem='MainWindowViewModel' (HashCode=44115416); target element is 'KeyBinding' (HashCode=29578451); target property is 'Command' (type 'ICommand')

System.Windows.Data 警告:80:BindingExpression (hash=2683661):TransferValue - 得到原始值 {DependencyProperty.UnsetValue}

System.Windows.Data Warning: 80 : BindingExpression (hash=2683661): TransferValue - got raw value {DependencyProperty.UnsetValue}

System.Windows.Data 警告:88:BindingExpression (hash=2683661):TransferValue - 使用回退/默认值

System.Windows.Data Warning: 88 : BindingExpression (hash=2683661): TransferValue - using fallback/default value

System.Windows.Data 警告:89:BindingExpression (hash=2683661):TransferValue - 使用最终值

System.Windows.Data Warning: 89 : BindingExpression (hash=2683661): TransferValue - using final value

我看到这篇文章和这是同一个问题,但是,接受的答案没有任何代码(我试图通过链接,但是,提供的方法对我没有帮助)

I see this post and it is it is the same question, however, accepted answer does not have any code(I've tried to go through link, however, the offered approach didn't help me)

错误说:

System.Windows.Data 错误:40:BindingExpression 路径错误:在对象"MainWindowViewModel"上找不到EditLeafCommand"属性

System.Windows.Data Error: 40 :BindingExpression path error: 'EditLeafCommand' property not found on 'object' ''MainWindowViewModel'

但是我怎样才能定向到 EditLeadViewModel?

But how can I direct to EditLeadViewModel?

如何从 LeafViewModel 调用 EditLeafCommand 并发送参数?

How can I call EditLeafCommand from LeafViewModel and send parameter?

推荐答案

Andy ONeill 已经解决了这个问题.我对他的解决方案感到非常兴奋:).赶紧把这个解决方案分享给大家:

Andy ONeill has solved this problem. I am really excited by his solution:). I hurry to share this solution with you:

绑定到 LeafViewModel 中的命令:

<TreeView  ItemsSource="{Binding Leafs}" Name="tv">
   <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type local:LeafViewModel}" 
                                      ItemsSource="{Binding Children}">
         <StackPanel Orientation="Horizontal">
            <Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
               <TextBox Text="{Binding LeafName}" Tag="{Binding DataContext, 
                  RelativeSource={RelativeSource Self}}" Background="Transparent">
                  <TextBox.ContextMenu>
                     <ContextMenu DataContext="{Binding PlacementTarget.Tag, 
                                     RelativeSource={RelativeSource Self}}">
                        <MenuItem Command="{Binding EditLeafCommand}" 
                         CommandParameter="{Binding ALeaf}" Header="Edit" />
                     </ContextMenu>
                  </TextBox.ContextMenu>
               </TextBox>
            </StackPanel>
         </HierarchicalDataTemplate>
      </TreeView.Resources>
    <TreeView.InputBindings>
    <KeyBinding Key="F2" Command="{Binding SelectedItem.EditLeafCommand, ElementName=tv}"/>
    </TreeView.InputBindings>
</TreeView>

这篇关于TreeView 的 HierarchicalDataTemplate 中的 KeyBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆