wpf 用扩展器替换 Treeview +-

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11815575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:50:48  来源:igfitidea点击:

Replacing Treeview +- with Expander

wpftreeviewwpf-controls

提问by surpavan

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

我正在尝试从树视图中删除节点符号 +- 并将它们替换为 Expander。以下是我的 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.

我需要删除 +- 图标并使扩展器工作。你能否请教如何完成这项工作。谢谢你。

回答by 123 456 789 0

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>

回答by H.B.

In the ItemContainerStyleset a new Templatefor the TreeViewItems.

ItemContainerStyle设置一个新TemplateTreeViewItems.