WPF 双击 TreeviewItem 子节点

发布于 2024-10-05 00:02:07 字数 650 浏览 5 评论 0原文

我在树视图中有一个树视图项目,该项目将有一个绑定到它的列表:

        <TreeViewItem Name="tviOffline" Foreground="Red" FontWeight="Bold"
                      Header="Offline">
            <TreeViewItem.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Buddy}">
                    <StackPanel>
                        <TextBlock Text="{Binding Nick}" FontSize="10" Foreground="#8CFFD528" />
                    </StackPanel>
                </DataTemplate>
            </TreeViewItem.ItemTemplate>
        </TreeViewItem>

我无法弄清楚如何让它的每个子项都有双击事件。

任何帮助表示赞赏。非常感谢。

I have a treeview Item as such in a treeview that will have a list bound to it:

        <TreeViewItem Name="tviOffline" Foreground="Red" FontWeight="Bold"
                      Header="Offline">
            <TreeViewItem.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Buddy}">
                    <StackPanel>
                        <TextBlock Text="{Binding Nick}" FontSize="10" Foreground="#8CFFD528" />
                    </StackPanel>
                </DataTemplate>
            </TreeViewItem.ItemTemplate>
        </TreeViewItem>

I cannot figure out how to get each of its childs to have a double click event.

any help is appreciated. thanks much.

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

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

发布评论

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

评论(3

怀中猫帐中妖 2024-10-12 00:02:07
<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <EventSetter Event="MouseDoubleClick" Handler="OnItemMouseDoubleClick" />
        ...
<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <EventSetter Event="MouseDoubleClick" Handler="OnItemMouseDoubleClick" />
        ...
梦里泪两行 2024-10-12 00:02:07
<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <EventSetter Event="MouseDoubleClick" Handler="OnItemMouseDoubleClick" />
        ...

然后,必须按如下方式编写处理程序,以防止在连续的父 TreeViewItems 上触发双击:

   private void OnItemMouseDoubleClick(object sender, MouseButtonEventArgs args)
    {
        if (sender is TreeViewItem)
        {
            if (!((TreeViewItem)sender).IsSelected)
            {
                return;
            }
        }

        .... do stuff.

    }

感谢 Aurelien Ribon 完成了 90% 的工作。双击问题在 Stack Exchange 上的其他帖子中似乎是众所周知的。只需将两种解决方案合并为一个答案即可。

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <EventSetter Event="MouseDoubleClick" Handler="OnItemMouseDoubleClick" />
        ...

And THEN, the handler has to be written as follows in order to prevent the double-click from firing on successive parent TreeViewItems:

   private void OnItemMouseDoubleClick(object sender, MouseButtonEventArgs args)
    {
        if (sender is TreeViewItem)
        {
            if (!((TreeViewItem)sender).IsSelected)
            {
                return;
            }
        }

        .... do stuff.

    }

Thanks to Aurelien Ribon for getting 90% of the way there. The double-click problem seems to be well-known in other postings on Stack Exchange. Just consolidating both solutions into one answer.

穿越时光隧道 2024-10-12 00:02:07

这是我设法让它适用于所有情况的唯一方法:

    void MyTreeView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var clickedItem = TryGetClickedItem(myTreeView, e);
        if (clickedItem == null)
            return;

        e.Handled = true; // to cancel expanded/collapsed toggle
        DoStuff(clickedItem);
    }

    TreeViewItem TryGetClickedItem(TreeView treeView, MouseButtonEventArgs e)
    {
        var hit = e.OriginalSource as DependencyObject;
        while (hit != null && !(hit is TreeViewItem))
            hit = VisualTreeHelper.GetParent(hit);

        return hit as TreeViewItem;
    }

This is the only way I managed to get it to work for all the cases:

    void MyTreeView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var clickedItem = TryGetClickedItem(myTreeView, e);
        if (clickedItem == null)
            return;

        e.Handled = true; // to cancel expanded/collapsed toggle
        DoStuff(clickedItem);
    }

    TreeViewItem TryGetClickedItem(TreeView treeView, MouseButtonEventArgs e)
    {
        var hit = e.OriginalSource as DependencyObject;
        while (hit != null && !(hit is TreeViewItem))
            hit = VisualTreeHelper.GetParent(hit);

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