2017-04-06 50 views
-1

My ObservableCollection來自Type FUClass。在這個類中是一個FUSubClass的列表。 ListView Itemsource綁定到ObservableCollection FUList。ItemContainerStyle for Treeview使用類與子類列表

我的TreeView顯示GroupName,並且有一個TreeViewItem和ItemName。

問題是隻有1個TreeViewItem與FUSubClassList中的所有項目都顯示出來,但是我希望每個Item都有一個TreeViewItem。 不知道如何在ItemContainerStyle中實現該功能。

XAML ItemContainerStyle:

<Style PresentationOptions:Freeze="True" TargetType="{x:Type ListViewItem}"> 
     <Setter Property="Height" Value="Auto" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListViewItem}"> 
        <TreeView Width="1248" MinHeight="50"> 
         <TreeViewItem> 
          <TreeViewItem.Header> 
           <StackPanel Orientation="Horizontal"> 
            <ContentPresenter Content="{Binding GroupName}" RecognizesAccessKey="False" /> 
           </StackPanel> 
          </TreeViewItem.Header> 
          <TreeViewItem ItemsSource="{Binding FUSubClassList}"> 
           <TreeViewItem.Header> 
            <Grid> 
             <ContentPresenter Content="{Binding ItemName}" RecognizesAccessKey="False" /> 
            </Grid> 
           </TreeViewItem.Header> 
          </TreeViewItem> 
         </TreeViewItem> 
        </TreeView> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

C#

public class FUClass : INotifyPropertyChanged 
    { 
     private string groupName; 
     public string GroupName 
     { 
      get { return this.groupName; } 
      set 
      { 
       if (this.groupName != value) 
       { 
        this.groupName = value; 

        this.OnPropertyChanged(nameof(GroupName)); 
       } 
      } 
     } 
     private ObservableCollection<FUSubClass> FUSubClassList = new ObservableCollection<FUSubClass>(); 
     public ObservableCollection<FUSubClass> FUSubClassList 
     { 
      get { return FUSubClassList; } 
      set { FUSubClassList = value; } 
     } 

     public class FUSubClass : INotifyPropertyChanged 
     { 
      private string itemName; 
      public string ItemName 
      { 
       get { return this.itemName; } 
       set 
       { 
        if (this.itemName != value) 
        { 
         this.itemName = value; 

         this.OnPropertyChanged(nameof(ItemName)); 
        } 
       } 
      } 
      public event PropertyChangedEventHandler PropertyChanged; 
      protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
      { 
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private ObservableCollection<FUClass> FUList = new ObservableCollection<FUClass>(); 

UI

**UI**

回答

1

跳過ItemContainerStyle和定義數據模板,爲您的數據類型,例如:

<TreeView ItemsSource="{Binding FUList}"> 
    <TreeView.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type local:FUClass}" ItemsSource="{Binding FUSubClassList}"> 
      <TextBlock Text="{Binding GroupName}" /> 
     </HierarchicalDataTemplate> 
     <DataTemplate DataType="{x:Type local:FUSubClass}"> 
      <TextBlock Text="{Binding ItemName}" /> 
     </DataTemplate> 
    </TreeView.Resources> 
</TreeView> 

請提供一個最小的,完整的,並且可驗證的例子,你需要任何進一步的幫助:https://stackoverflow.com/help/mcve