Home > Back-end >  C# WPF TreeViewItem MouseDoubleClick event on TreeViewItem with TreeViewItem.Header
C# WPF TreeViewItem MouseDoubleClick event on TreeViewItem with TreeViewItem.Header

Time:11-11

I have a TreeView which contains mixture of different levels of TreeViewItem's.

Below works perfectly:

            <TreeViewItem Tag="Link" MouseDoubleClick="TreeViewItem_MouseDoubleClick">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="Link" Margin="0 0 5 0"/>
                            <TextBlock Text="Link"/>
                        </StackPanel>
                    </TreeViewItem.Header>
            </TreeViewItem>

But when it comes to ones where they have a TreeViewItem.Header I'm not able to set the MouseDoubleClick event correctly, see below:

            <TreeViewItem IsExpanded="True" MouseDoubleClick="TreeViewItem_MouseDoubleClick">
                <TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="HelpCircleOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Help" FontWeight="Bold"/>
                    </StackPanel>
                </TreeViewItem.Header>
                <ContentControl>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="BookOpenOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Technical Guide"/>
                    </StackPanel>
                </ContentControl>
            </TreeViewItem>

Issue with the above is that it triggers on both the Help header and the Technical Guide child.

I cannot add the event onto the <StackPanel> as it doesn't support it, so I wrapped it with <ContentControl> and tried adding the event to that, but it doesn't seem to trigger at all.

Is there any way of only having the "Technical Guide" triggering the event, but also adding in the Tag="" property as per the first code snippet as I am checking for that value in the TreeViewItem_MouseDoubleClick event?

EDIT

As requested providing code for the MouseDoubleClick event:

    private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (sender is TreeViewItem item)
        {
            var header = item.Tag as string;
            switch (header)
            {
                case "Link":
                       //Open new window code
                    break;
             }
     }

CodePudding user response:

instead of trapping click from treeview, why you dont trap click from textblock?

        <TreeViewItem IsExpanded="True"   >
            <TreeViewItem.Header>
                <StackPanel Orientation="Horizontal">
                    <materialDesign:PackIcon Kind="HelpCircleOutline" Margin="0 0 5 0"/>
                    <TextBlock Text="Help" FontWeight="Bold" MouseDown="TextBlock_MouseDown"/>
                </StackPanel>
            </TreeViewItem.Header>
            <StackPanel Orientation="Horizontal">
                <materialDesign:PackIcon Kind="BookOpenOutline" Margin="0 0 5 0"/>
                <TextBlock Text="Technical Guide" MouseDown="TextBlock_MouseDown"/>
            </StackPanel>
        </TreeViewItem>

    private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // simulate doubleclick
        if (e.LeftButton == MouseButtonState.Pressed && e.ClickCount == 2)
        {
            System.Diagnostics.Debug.WriteLine((TextBlock)sender.Text);
        }
    }

CodePudding user response:

I cannot add the event onto the <StackPanel> as it doesn't support it ...

Yes, you if you set the Background property to Transparent and handle the MouseLeftButtonDown event and check the ClickCount property of the MouseButtonEventArgs:

private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
        //double-click detected
    }
}

XAML:

<StackPanel Orientation="Horizontal" 
            Background="Transparent"
            MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">
    <TextBlock Text="Technical Guide"/>
</StackPanel>
  • Related