Home > Blockchain >  WPF C# TreeView get text of selected item
WPF C# TreeView get text of selected item

Time:11-10

I've got the following TreeView:

<TreeView x:Name="TreeView" 
                  MouseDoubleClick="TreeView_MouseDoubleClick"
                  Width="350">
            <TreeViewItem IsExpanded="True">
                <TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="CogOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Configuration" FontWeight="Bold"/>
                    </StackPanel>
                </TreeViewItem.Header>
                <TreeViewItem IsExpanded="True">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="TruckDeliveryOutline" Margin="0 0 5 0"/>
                            <TextBlock Text="Items"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ArrowUpBoldBoxOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Configuration"/>
                    </StackPanel>
                </TreeViewItem>
                <TreeViewItem IsExpanded="True">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="FileDocumentOutline" Margin="0 0 5 0"/>
                            <TextBlock Text="List"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ClipboardMultipleOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Management"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ClipboardListOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Management"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ClipboardPlusOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Sundry Items"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="RelationOneToMany" Margin="0 0 5 0"/>
                        <TextBlock Text="Sundry Addition"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ClipboardPlusOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Sundry Addition"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="RelationOneToMany" Margin="0 0 5 0"/>
                        <TextBlock Text="Addition Relations"/>
                    </StackPanel>
                </TreeViewItem>
            </TreeViewItem>
            <TreeViewItem IsExpanded="True">
                <TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="FormatListBulleted" Margin="0 0 5 0"/>
                        <TextBlock Text="Tasks" FontWeight="Bold"/>
                    </StackPanel>
                </TreeViewItem.Header>
                <TreeViewItem IsExpanded="True">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="TruckDeliveryOutline" Margin="0 0 5 0"/>
                            <TextBlock Text="Items"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ChartFinance" Margin="0 0 5 0"/>
                        <TextBlock Text="(Converted)"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="ChartFinance" Margin="0 0 5 0"/>
                        <TextBlock Text="(Each)"/>
                    </StackPanel>
                </TreeViewItem>
                <TreeViewItem>
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="CurrencyGbp" Margin="0 0 5 0"/>
                            <TextBlock Text="Material"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                </TreeViewItem>
                <TreeViewItem IsExpanded="True">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="CurrencyGbp" Margin="0 0 5 0"/>
                            <TextBlock Text="Product"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="Check" Margin="0 0 5 0"/>
                        <TextBlock Text="Validation"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="Update" Margin="0 0 5 0"/>
                        <TextBlock Text="Update All"/>
                    </StackPanel>
                </TreeViewItem>
                <TreeViewItem>
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="Salesforce" Margin="0 0 5 0"/>
                            <TextBlock Text="Bundle"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                </TreeViewItem>
                <TreeViewItem>
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="Calculator" Margin="0 0 5 0"/>
                            <TextBlock Text="Calculator"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                </TreeViewItem>
            </TreeViewItem>
                <TreeViewItem IsExpanded="True">
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="HelpCircleOutline" Margin="0 0 5 0"/>
                            <TextBlock Text="Help" FontWeight="Bold"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <materialDesign:PackIcon Kind="BookOpenOutline" Margin="0 0 5 0"/>
                        <TextBlock Text="Guide"/>
                    </StackPanel>
                </TreeViewItem>
                <TreeViewItem>
                    <TreeViewItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <materialDesign:PackIcon Kind="Link" Margin="0 0 5 0"/>
                            <TextBlock Text="Link"/>
                        </StackPanel>
                    </TreeViewItem.Header>
                </TreeViewItem>
            
        </TreeView>

It is to be used as navigation in a menu form, but I am struggling to create an event which would trigger when something is clicked and then return the text value that is displayed on the item in the tree view.

I've tried the following articles:

Getting the value of the Selected Child Nodes on click Event in WPF TreeView

How to add WPF treeView Node Click event to get the node value

Using solutions from both, don't work for me as I am not able to access the item.header in one and in other cases it doesn't seem to correctly create the TreeViewItem item = sender as TreeViewItem.

Have I over-complicated my hard-coded TreeView to a point where I am unable to create such event to return me the text value of the clicked item?

CodePudding user response:

You can specify Tag property for each TreeViewItem. In MouseClick event handler -> you will able to convert sender to TreeViewItem.. and then you will be able to take Tag property of it

<TreeViewItem Tag="Link" MouseLeftButtonDown="ClickItem_Handler">
 ...
</TreeViewItem>

in codebehind of your form

protected void ClickItem_Handler(object sender, MouseEventArgs args)
{
    if (sender is TreeViewItem item){
        var header = item.Tag as string;
    }
}
  • Related