具有嵌套ObservableCollections的TreeView

时间:2017-09-21 05:56:49

标签: c# .net wpf xaml treeview

我有一个ListOfApps<(App>)包括一些属性,如Name,Version,CurrentUser或CurrentOwner(都包括ObservableCollections),并希望在TreeView中显示它们。这些是更好解释的接口:

public interface IApp
{
    string Name { get; }
    string Version { get; }
    User CurrentUser { get; }
    Owner CurrentOwner { get; }
}

public interface IUser
{
    // A property of Info is e.g. Description
    ObservableCollection<Info> ListOfInfos { get; }
}

public interface IOwner
{
    // A property of Member is e.g. Name
    ObservableCollection<Member> ListOfMembers { get; }
}

Treeview的结果应如下所示:

- App1                      Name (String)
-- Version                  Version (String)
-- CurrentUser              App.CurrentUser (User) -> Name (String)
---  Info1                  App.CurrentUser.ListOfInfos (ObservableCollection<Info>) -> Description (String)
---  Info2                  App.CurrentUser.ListOfInfos (ObservableCollection<Info>) -> Description (String)
---  Info3                  App.CurrentUser.ListOfInfos (ObservableCollection<Info>) -> Description (String)
-- CurrentOwner             App.CurrentOwner (Owner) -> Name (String)
---  Member1                App.CurrentOwner.ListOfMembers (ObservableCollection<Member>) -> Name (String)
---  Member2                App.CurrentOwner.ListOfMembers (ObservableCollection<Member>) -> Name (String)

- App2                      Name (String)
-- Version                  Version (String)
-- CurrentUser              App.CurrentUser (User) -> Name (String)
---  Info1                  App.CurrentUser.ListOfInfos (ObservableCollection<Info>) -> Description (String)
---  Info2                  App.CurrentUser.ListOfInfos (ObservableCollection<Info>) -> Description (String)
-- CurrentOwner             App.CurrentOwner (Owner) -> Name (String)
---  Member1                App.CurrentOwner.ListOfMembers (ObservableCollection<Member>) -> Name (String)
---  Member2                App.CurrentOwner.ListOfMembers (ObservableCollection<Member>) -> Name (String)
---  Member3                App.CurrentOwner.ListOfMembers (ObservableCollection<Member>) -> Name (String)

- App3
- ...

我试图通过一些HierarchicalDataTemplate来实现它,但没有任何成功。这是我的方法:

<TreeView ItemsSource="{Binding ListOfApps}">

    <TreeView.Resources>

        <!-- App Items -->
        <DataTemplate DataType="{x:Type appConfig:App}">
                <TextBlock Text="{Binding Name}" />
        </DataTemplate>

        <!-- App Item: Version -->
        <DataTemplate DataType="{x:Type appConfig:App}">
                <TextBlock Text="{Binding Version}" />
        </DataTemplate>

        <!-- CurrentUser -->
        <HierarchicalDataTemplate DataType="{x:Type appConfig:User}" ItemsSource="{Binding ListOfInfos}">
                <TextBlock Text="{Binding CurrentUser.Name}" />
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type appConfig:Info}">
                <TextBlock Text="{Binding Description}" />
        </DataTemplate>

        <!-- CurrentOwner -->
        <HierarchicalDataTemplate DataType="{x:Type appConfig:Owner}" ItemsSource="{Binding ListOfMembers}">
                <TextBlock Text="{Binding CurrentOwner.Name}" />
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type appConfig:Member}">
                <TextBlock Text="{Binding Name}" />
        </DataTemplate>

    </TreeView.Resources>
</TreeView>

0 个答案:

没有答案