命令绑定在分层数据表中 [英] Command Binding in hierarchical datatemplate

查看:204
本文介绍了命令绑定在分层数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式中有菜单。我使用分层数据模板显示它:

I have Menu in my app. I'm visualizing it using hierarchical data template:

    <MenuItem Header="Main menu" ItemsSource="{Binding ApplicationMenu}" >
        <MenuItem.ItemTemplate>                    
            <HierarchicalDataTemplate DataType="{x:Type tm:RMenuItem}" 
                                      ItemsSource="{Binding Path=ChildrenItems}">                        
                <MenuItem Header="{Binding Name}" Command="{Binding RunOperationCommand}" />
            </HierarchicalDataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>

菜单看起来像它应该,但每个菜单项的命令不会启动!甚至更多 - 它没有界限,这可以在调试器中看到:ICUSTand的get访问器属性从未执行过。
为什么会发生这样的情况?

menu looks like as it should, but Command for each menu item is not fired! Even more - it is not bounded, which could be seen in debugger: get accessor of ICommand Property has been never executed. Why it happens so?

照常工作完美:

<Menu>
    <MenuItem Header="SomeHeader" Command="{Binding RunOperationCommand}"/>
<Menu>


推荐答案

问题是,在第二个代码片段中,您将 MenuItem.Command 绑定到父的数据上下文,其中包含 RunOperationCommand 定义。而在使用 HierarchicalDataTemplate 的第一个示例中,您绑定到本地DataContext(它是一个菜单项)。它没有合适的属性,因此绑定失败。

The difference between the first and the second example in your question is that in the second code snippet you are binding MenuItem.Command to the parent's data context, which has the RunOperationCommand defined. Whereas in the first example with the HierarchicalDataTemplate you are binding to the "local" DataContext, which is a menu item. It doesn't have the appropriate property, so the binding fails.

您有几个选项:


  • 一个是使用命令属性扩展菜单项,就像您在答案中所做的那样;

  • 在视觉树中绑定到相关源,数据上下文与命令,例如假设该命令在您的窗口的DataContext中:

    <MenuItem Header="Main menu" ItemsSource="{Binding ApplicationMenu}" >
        <MenuItem.ItemTemplate>                    
            <HierarchicalDataTemplate DataType="{x:Type tm:RMenuItem}" 
                                      ItemsSource="{Binding Path=ChildrenItems}">                        
                <MenuItem Header="{Binding Name}" 
                          Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RunOperationCommand}" 
                />
            </HierarchicalDataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>







  • ,类似于此博文方法


    • Make a StaticResource from your command, similar to this blog post approach
    • <Window.Resources>
           <coreView:CommandReference x:Key="RunOperationCommand"
                                      Command="{Binding RunOperationCommand}" />
      </Window.Resources>
      
          <MenuItem Header="Main menu" ItemsSource="{Binding ApplicationMenu}" >
              <MenuItem.ItemTemplate>                    
                  <HierarchicalDataTemplate DataType="{x:Type tm:RMenuItem}" 
                                            ItemsSource="{Binding Path=ChildrenItems}">                        
                      <MenuItem Header="{Binding Name}" 
                                Command="{StaticResource RunOperationCommand}" 
                      />
                  </HierarchicalDataTemplate>
              </MenuItem.ItemTemplate>
          </MenuItem>
      

      这篇关于命令绑定在分层数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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