Treeview是否使用命令绑定进行展开/折叠? [英] Does Treeview use command bindings for expand/collapse?

查看:159
本文介绍了Treeview是否使用命令绑定进行展开/折叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF树视图响应 + -的击键,以展开和折叠树中的节点.太好了!

The WPF Treeview responds to + and - keystrokes to expand and collapse nodes in the tree. Great!

是否存在可以将工具栏按钮或菜单项绑定到树视图中以执行相同操作的现有命令?我没有在stock命令常量中看到任何与扩展/折叠有关的信息.

Is there an existing command I can bind my toolbar buttons or menu items to to perform the same actions in the treeview? I don't see anything related to expand/collapse in the stock command constants.

推荐答案

TreeView通过将ToggleButton.IsChecked绑定到ControlTemplate中的TreeViewItem.IsExpanded来用鼠标处理TreeViewItem的扩展,并使用覆盖TreeViewItem.OnKeyDown中的键盘.因此,不,它在实现中不使用命令.

The TreeView handles the expansion of a TreeViewItem with the mouse by binding ToggleButton.IsChecked to TreeViewItem.IsExpanded in the ControlTemplate and handles expansion with the keyboard in an override TreeViewItem.OnKeyDown. So, no, it doesn't use commands in its implementation.

但是您可以轻松添加自己的命令.在此示例中,我向TreeView添加了一个行为,以使其响应标准的OpenClose应用程序命令:

But you can add your own commands without much effort. In this example, I've added a behavior to a TreeView so that it responds to the standard Open and Close application commands:

<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="Open" CommandTarget="{Binding ElementName=treeView1}" Command="Open"/>
        <MenuItem Header="Close" CommandTarget="{Binding ElementName=treeView1}" Command="Close"/>
    </Menu>
    <TreeView>
        <i:Interaction.Behaviors>
            <local:TreeViewCommandsBehavior/>
        </i:Interaction.Behaviors>
        <TreeViewItem Header="Root">
            <TreeViewItem Header="Item1">
                <TreeViewItem Header="Subitem1"/>
                <TreeViewItem Header="Subitem2"/>
            </TreeViewItem>
            <TreeViewItem Header="Item2">
                <TreeViewItem Header="Subitem3"/>
                <TreeViewItem Header="Subitem4"/>
            </TreeViewItem>
        </TreeViewItem>
    </TreeView>
</DockPanel>

这是使之起作用的行为:

and here is the behavior that makes that work:

public class TreeViewCommandsBehavior : Behavior<TreeView>
{
    private TreeViewItem selectedTreeViewItem;

    protected override void OnAttached()
    {
        AssociatedObject.AddHandler(TreeViewItem.SelectedEvent, new RoutedEventHandler(TreeViewItem_Selected));
        AssociatedObject.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, CommandExecuted));
        AssociatedObject.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CommandExecuted));
    }

    private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
    {
        selectedTreeViewItem = e.OriginalSource as TreeViewItem;
    }

    private void CommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        bool expand = e.Command == ApplicationCommands.Open;
        if (selectedTreeViewItem != null)
            selectedTreeViewItem.IsExpanded = expand;
    }
}

如果您不熟悉行为,请首先添加以下名称空间:

If you are not familiar with behaviors, first add this namespace:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

并将相应的引用添加到您的项目中.

and add the corresponding reference to your project.

这篇关于Treeview是否使用命令绑定进行展开/折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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