Treeview 是否使用命令绑定来展开/折叠?

发布于 2024-10-13 08:04:29 字数 141 浏览 2 评论 0原文

WPF Treeview 响应 +- 击键来展开和折叠树中的节点。伟大的!

是否有一个现有命令可以绑定工具栏按钮或菜单项以在树视图中执行相同的操作?我在库存命令常量中没有看到任何与展开/折叠相关的内容。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-10-20 08:04:29

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

但您可以毫不费力地添加自己的命令。在此示例中,我向 TreeView 添加了一个行为,以便它响应标准的 OpenClose 应用程序命令:

<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>

以下是使其起作用的行为:

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;
    }
}

如果您不熟悉行为,请首先添加此命名空间:

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文