Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking for a way to add button to a selected item in TreeView in WPF, but haven't found any useful solutions.

I am importing data from an XML file:

What I have tried:

C#
XmlDocument doc = recources.Xml_load();

            foreach (XmlNode n in doc.SelectNodes("/LMSProjekt/Projekt"))
            {
                XmlNode n_name = n.SelectSingleNode("Name");
                TreeViewItem itm = new TreeViewItem();
                //itm.Items.Add(cb);
                itm.Header = n_name.InnerText;
                Treeview.Items.Add(itm);

                foreach (XmlNode n_desc in n.SelectNodes("Prozess"))
                {
                    n_name = n_desc.SelectSingleNode("ProzessName");
                    TreeViewItem sub_itm = new TreeViewItem();
       sub_itm.Header = n_name.InnerText; //i would like to add a button HERE 
                    itm.Items.Add(sub_itm);

                    foreach (XmlNode n_desc2 in n_desc.SelectNodes("description"))
                    {   
                        TreeViewItem sub2_itm = new TreeViewItem();
                        sub2_itm.Header = n_desc2.InnerText ;
                        sub_itm.Items.Add(sub2_itm);
                        selected = n_desc2.InnerText;
                    }  
                }
            }
Posted
Updated 19-Oct-16 4:18am
v2
Comments
[no name] 17-Oct-16 11:14am    
Did you add a template for your treeview item to include a button?
Member 12785541 17-Oct-16 11:27am    
actually not , how can i do this ? i am new in C#
[no name] 17-Oct-16 13:05pm    
You define a template for your treeview items. Then you assign your template to your items. http://www.codeproject.com/Articles/236621/WPF-Treeview-Styling-and-Template-Binding-using-MV might help
johannesnestler 24-Oct-16 10:41am    
would be nice if you vote for solution, or tell us you have solved it...

1 solution

Hi Member,

Have a look at this to get the idea:

XML
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication2"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    x:Class="WpfApplication2.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="350"
    Width="525">

    <Window.Resources>
        <!--Because we need to convert the visibility enum value to a boolean (the IsSelected property of the TreeViewItem) we need a converter - let's use the default one, we create a window-level resource out of it-->
        <BooleanToVisibilityConverter
            x:Key="BooleanToVisibilityConverter">
        </BooleanToVisibilityConverter>
    </Window.Resources>
    <Grid>

        <TreeView>
            <!--Define an ItemTemplate like you wish for you tree nodes (could use different ones, for different kind of nodes by setting a DataType for the DataTemplate)-->
            <TreeView.ItemTemplate>
                <DataTemplate>

                    <!--As example we use a combination of a TextBlock and a Button stacked horizontally (add margins and so on to get a nicer look)-->
                    <StackPanel
                        Orientation="Horizontal">
                        <TextBlock
                            Text="{Binding}" />
                        <!--here is the trick: Every added item (in this example just strings) will be wrapped in a TreeViewItem by the Treeview, so we bind the visibility of the button
                            to the IsSelected property of its "upper" TreeViewItem, then we use the converter we defined earlier to convert the boolean value to a meaningful Visibility enum value -->
                        <Button
                            Content="clickme"
                            Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Converter={StaticResource BooleanToVisibilityConverter}}">
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>

            <TreeViewItem></TreeViewItem>

            <!--Just some sample items (in real you will most likely use ItemsSource property to bind data-->
            <System:String>bla</System:String>
            <System:String>bla</System:String>
            <System:String>bla</System:String>
            <System:String>bla</System:String>
            <System:String>bla</System:String>


        </TreeView>

    </Grid>
</Window>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900