surpavan
surpavan

Reputation: 1412

Replacing Treeview +- with Expander

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.

Upvotes: 1

Views: 7726

Answers (2)

123 456 789 0
123 456 789 0

Reputation: 10865

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>

Upvotes: 4

brunnerh
brunnerh

Reputation: 184544

In the ItemContainerStyle set a new Template for the TreeViewItems.

Upvotes: 3

Related Questions