-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