简体   繁体   中英

How to add a MouseDoubleClick event on C# to a TreeViewItem

I'm new in WPF and C# programming.

I'm trying to create from code a TreeView to add data from my database. 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. All of the data is formed as string.

I add them into my TreeView in this way:

// 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示例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" :

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:

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

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; // 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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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