WPF xaml 中的 Treeview 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14451313/
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 07:09:39  来源:igfitidea点击:

Treeview Header in WPF xaml

.netwpfxamltreeview

提问by BinaryMee

I am having a treeview in my wpf application which dynamically binds data from my viewmodel.

我的 wpf 应用程序中有一个树视图,它从我的视图模型动态绑定数据。

<TreeView Grid.Column="0"
          Grid.Row="0"
          ItemsSource="{Binding Main}"
          HorizontalAlignment="Stretch"
          Name="treeView1"
          VerticalAlignment="Stretch"
          Height="auto"
          Width="auto"
          SelectedItemChanged="treeView1_SelectedItemChanged"
          HorizontalContentAlignment="Stretch"
          VerticalContentAlignment="Top"
          Margin="0,0,5,5">
  <TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="IsExpanded"
              Value="{Binding IsExpanded, Mode=TwoWay}" />
      <Setter Property="IsSelected"
              Value="{Binding IsSelected, Mode=TwoWay}" />
      <Setter Property="FontWeight"
              Value="Normal" />
      <Style.Triggers>
        <Trigger Property="IsSelected"
                 Value="True">
          <Setter Property="FontWeight"
                  Value="Bold" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TreeView.ItemContainerStyle>
  <TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:MViewModel}"
                              ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Mname}" />
      </StackPanel>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:FViewModel}"
                              ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding FName}" />
      </StackPanel>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:CViewModel}"
                              ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Com}" />
      </StackPanel>
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type local:LViewModel}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Lo}" />
      </StackPanel>
    </DataTemplate>
  </TreeView.Resources>
</TreeView>

I need to add the header to the whole treeview say "Family" whose childs are the above treeview elements. How can i add header to the whole treeview.so that when i click that header it should display all Mname and so on. Please help me with this issue.

我需要将标题添加到整个树视图中说“家庭”,其子项是上述树视图元素。如何将标题添加到整个 treeview.so 以便当我单击该标题时它应该显示所有 Mname 等等。请帮我解决这个问题。

Edit:

编辑:

i have used

我用过了

  <HierarchicalDataTemplate  DataType="{x:Type TextBlock}" ItemsSource="{Binding Children}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Family" />
                        </StackPanel>
  </HierarchicalDataTemplate>

in my TreeviewResource. But still i can't assign the parent for the tree.

在我的 TreeviewResource 中。但我仍然无法为树分配父级。

Edit 2:

编辑2:

<TreeView>
   <TreeViewItem ItemsSource="{Binding Main}" Header="Family">
      <TreeViewItemContainerStyle>
      //..
      </TreeViewItemContainerStyle>
      <TreeView.Resources>
      //..
      </TreeView.Resources>
   </TreeViewItem>
</TreeView>

Its showing error.

它的显示错误。

But when i do like below,

但是当我喜欢下面的时候,

<TreeView>
       <TreeViewItemContainerStyle>
      //..
      </TreeViewItemContainerStyle>
      <TreeView.Resources>
      //..
      </TreeView.Resources>
   <TreeViewItem ItemsSource="{Binding Main}" Header="Family"/>
</TreeView>

The root item has been set. But Only the first set of child are displayed. Childs of child elements are not displaying.

根项已设置。但是只显示第一组孩子。子元素的子元素未显示。

回答by stukselbax

You can wrap your TreeViewwith Expanderelement:

您可以使用Expander元素包装TreeView

<Expander Header="Family">
    <TreeView>
        ...
    <TreeView>
</Expander>

But it is not clear from your question which element you want to use as a Header.

但是从您的问题中不清楚您想将哪个元素用作Header.

Edit:

编辑:

So, if you need a root element for a tree, you can add it explicitly in xaml:

所以,如果你需要一个树的根元素,你可以在xaml 中显式地添加它:

<TreeView> <!--ItemsSource you should set on a TreeViewItem-->
    <TreeViewItem ItemsSource="{Binding Main}" Header="Family">
        <TreeViewItem.ItemContainerStyle>
            ...
        </TreeViewItem.ItemContainerStyle>
        <TreeViewItem.Resources>
            ...
        </TreeViewItem.Resources>         
    </TreeViewItem>
<TreeView>