1

I am trying to remove the node signs +- from tree view and replace them with Expander. Following is my Xaml:

        <TreeView.Resources>
            <HierarchicalDataTemplate ItemsSource="{Binding Disks}" DataType="{x:Type local1:GenSet}">
                <Expander Header="{Binding Genre}" x:Name="exp" IsExpanded="False" >
                </Expander>
                <HierarchicalDataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
                        <Setter TargetName="exp" Property="IsExpanded"  Value="True"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="False">
                        <Setter TargetName="exp" Property="IsExpanded"  Value="False"/>
                    </DataTrigger>
                </HierarchicalDataTemplate.Triggers>

                <!--<TextBlock Text="{Binding Genre}"/>-->
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type local1:DiskPrime}">
                <TextBlock Text="{Binding Namee}"/>
            </DataTemplate>
        </TreeView.Resources>
    </TreeView>

I need to remove the +- icons and get the expander to work. Could you please advice how to get this done. Thank you.

H.B.
  • 166,899
  • 29
  • 327
  • 400
surpavan
  • 1,372
  • 7
  • 34
  • 66

2 Answers2

4

Play with this and modify it to satisfy your needs.

     <TreeView>
         <TreeView.Resources> 
           <Style TargetType="TreeViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander Header="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Header}">
                                <ContentPresenter Content="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Items[0]}"/>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
        <TreeViewItem Header="Test 1">
            <TreeViewItem Header="Child 1"><TextBox Text="Hello"></TextBox></TreeViewItem>
        </TreeViewItem>
        <TreeViewItem Header="Test 2"/>
        <TreeViewItem Header="Test 3"/>
     </TreeView>
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • 1
    Thank you. However, the selection change does not work, I think the mouse actions are not working it. – surpavan Aug 06 '12 at 06:42
3

In the ItemContainerStyle set a new Template for the TreeViewItems.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 1
    I am a newbie, could you be a bit more in detail or point to a sample or simple way to understand it. Thank you. – surpavan Aug 05 '12 at 10:20
  • Made some improvements, could you please tell me what to do next. – surpavan Aug 05 '12 at 10:49
  • @surpavan: Your templates are still messed up, the DataTemplate should not contain any expander, and in the ControlTemplate the Border with the Header content presenter should be inside `Expander.Header`, also add an `ItemsPresenter` as `Expander.Content` for the child items. – H.B. Aug 05 '12 at 10:57
  • Thanks, I posted the edited xaml in question, could you please tell me what to do next. – surpavan Aug 05 '12 at 11:58
  • 2
    @surpavan: This is **not** how this site works, first of all you *do not* edit the question until it no longer is one, secondly this is not a forum or chat, i will not walk you through this. Get the basics of [control authoring](http://msdn.microsoft.com/en-us/library/ms745025.aspx), look at [examples](http://msdn.microsoft.com/en-us/library/ms752048), get the [default template](http://stackoverflow.com/questions/1559261), try to understand how it works together, what elements and bindings are required, and transfer that knowledge to your own requirements, use your brain. – H.B. Aug 05 '12 at 12:22
  • Sure, Thank you for the time spent and for pointing me in right direction. – surpavan Aug 05 '12 at 13:07
  • @Chris: Yes, but explaining WPF basics in every single answer is not the solution. If some part of the answer requires additional knowledge people should look that up elsewhere. Less redundant, less error prone, less work. – H.B. Dec 18 '15 at 14:35