简体   繁体   English

如何在C#上将MouseDoubleClick事件添加到TreeViewItem

[英]How to add a MouseDoubleClick event on C# to a TreeViewItem

I'm new in WPF and C# programming. 我是WPF和C#编程的新手。

I'm trying to create from code a TreeView to add data from my database. 我正在尝试从代码中创建一个TreeView来添加数据库中的数据。 The data is divided in two parts: 数据分为两部分:

  • The name of the client 客户名称
  • Some dates 一些约会

For me, the first part of the data is the main TreeViewItem, and the second ones, the dates, hangs from the first in the TreeView. 对我来说,数据的第一部分是主TreeViewItem,第二部分是日期,它挂在TreeView的第一部分。 All of the data is formed as string. 所有数据都形成为字符串。

I add them into my TreeView in this way: 我以这种方式将它们添加到我的TreeView中:

// Adding new client to TreeView
TreeViewItem item = new TreeViewItem();
item.Header = entry.Key;
item.ItemsSource = entry.Value.ToArray(); // Adding also the dates
item.MouseDoubleClick += TreeViewItem_MouseDoubleClick; // Here is the problem
try
{
    Arbol_Clientes.Items.Add(item);
}
catch( Exception error)
{
    Console.WriteLine("ERROR: " + error.ToString());
}

The problem is, when I click on the date of the TreeView, the event is called by the Client Name, for example: 问题是,当我单击TreeView的日期时,该事件由“客户端名称”调用,例如:

TreeView示例1

When I double click on red date, the event get the blue TreeViewItem as the one that has called the handler, in the handler code, hijo is "ABM" instead of "/2019 0:00:00" : 当我双击红色日期时,该事件将蓝色TreeViewItem作为调用处理程序的事件显示出来,在处理程序代码中,hijo是"ABM"而不是"/2019 0:00:00"

Handler code: 处理程序代码:


private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs {

    var hijo = sender as TreeViewItem;

    ItemsControl parent = GetSelectedTreeViewItemParent(hijo);

    TreeViewItem treeitem = parent as TreeViewItem;
    string Nombre_Cliente = treeitem.Header.ToString();

    Etiqueta.Content = Nombre_Cliente + " " + hijo.Header.ToString();
}

The final result I want to achive with all these is something like this xaml: 我想用所有这些实现的最终结果是这样的xaml:

<TreeView Grid.Column="0" BorderThickness="0" x:Name="Arbol_Clientes">
                <TreeViewItem Header="ABM" IsExpanded="True">
                    <TreeViewItem Header="Enero 2019" MouseDoubleClick="TreeViewItem_MouseDoubleClick"/>
                    <TreeViewItem Header="Febrero 2019" MouseDoubleClick="TreeViewItem_MouseDoubleClick"/>
                </TreeViewItem>
                <TreeViewItem Header="VCF" IsExpanded="True">
                    <TreeViewItem Header="Enero 2019" MouseDoubleClick="TreeViewItem_MouseDoubleClick"/>
                </TreeViewItem>
            </TreeView>

You could get a reference to the parent TreeViewItem of the clicked element ( e.OriginalSource ) using the VisualTreeHelper class: 您可以使用VisualTreeHelper类获得对所单击元素( e.OriginalSource )的父TreeViewItem的引用:

private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var hijo = FindParent<TreeViewItem>(e.OriginalSource as DependencyObject);
    //...

}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

item.MouseDoubleClick += TreeViewItem_MouseDoubleClick; item.MouseDoubleClick + = TreeViewItem_MouseDoubleClick; // Here is the problem //这是问题

You're not getting events for the date items because you're not subscribing to those items. 您没有订阅日期项目,因为您没有订阅这些项目。 Only the top level nodes are attaching the event. 仅顶级节点将附加事件。 You'll want to subscribe to the event on the child items 您需要在子项目上订阅活动

Try the following instead: 请尝试以下操作:

// Adding new client to TreeView
TreeViewItem item = new TreeViewItem();
item.Header = entry.Key;
foreach (var date in entry.Value.ToArray())
{
    var child = new TreeViewItem();
    child.Header = date;
    child.MouseDoubleClick += TreeViewItem_MouseDoubleClick; // Only subscribe to the child
    item.Items.Add(child);
}
try
{
    Arbol_Clientes.Items.Add(item);
}
catch (Exception error)
{
    Console.WriteLine("ERROR: " + error.ToString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM