从两个单独的列表生成树视图,其中子对象具有父对象的id

时间:2015-01-16 11:00:27

标签: wpf treeview multibinding imultivalueconverter

我遇到了问题,我在ViewModel中有两个单独的ObservableCollection。一个列表包含以下对象:

public class TypeCategory {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

另一个清单是:

public class Type {
    public string Name { get; set; }
    public string Description { get; set; }
    public int CategoryId { get; set; }
}

ViewModel就像:

public class TheViewModel {
    public ObservableCollection<TypeCategory > TypeCategories { get; private set; }
    public ObservableCollection<Type> Types { get; private set; }
}

TypeTypeCategory的childNode,其中父节点的连接是CategoryId。 是否可以通过使用例如multibinding和converter来连接两个列表来生成TreeView? 或者是扩展父类以包含它的孩子的唯一解决方案?

1 个答案:

答案 0 :(得分:1)

您可以尝试这样:

使用Dictionary<string,List<string>> TypeCategory Name作为Key&amp;在此List<string>下包含Type Names的{​​{1}}。

Category

public class TheViewModel { public ObservableCollection<TypeCategory> TypeCategories { get; set; } public ObservableCollection<Type> Types { get; set; } public Dictionary<string,List<string>> TreeViewItemSource { get; set; } public TheViewModel() { TypeCategories = new ObservableCollection<TypeCategory>(); Types = new ObservableCollection<Type>(); TreeViewItemSource = new Dictionary<string, List<string>>(); for (int i = 0; i < 10; i++) { TypeCategories.Add(new TypeCategory() { Id = i, Name = "TypeCategory "+ i, Description = string.Empty }); } for (int i = 0; i < 50; i++) { Types.Add(new Type() { CategoryId = i / 5, Name = "Type " + i, Description = string.Empty }); } foreach (TypeCategory item in TypeCategories) { List<string> list = Types.Where(p => p.CategoryId == item.Id).Select(p=>p.Name).ToList<string>(); TreeViewItemSource.Add(item.Name, list); } } }

XAML

希望这会对你有所帮助。

<TreeView Name="treeView1" ItemsSource="{Binding TreeViewItemSource}" > <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=Value}"> <TextBlock FontWeight="Bold" Text="{Binding Path=Key}" /> <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding }"/> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>

如果您不想创建字典,则需要在UPDATE上应用MultiBinding

HierarchicalDataTemplate's ItemSource

CategoryItemsConverter:

xmlns:local="clr-namespace:assembly_Name" 
.....
<TreeView Name="treeView1" ItemsSource="{Binding TypeCategories}" Tag="{Binding Types}">
    <TreeView.Resources>
        <local:CategoryItemsConverter x:Key="CategoryItemsConverter"/>
    </TreeView.Resources>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate >
            <HierarchicalDataTemplate.ItemsSource>
                <MultiBinding Converter="{StaticResource CategoryItemsConverter}">
                    <Binding Path="Tag" ElementName="treeView1"/>
                    <Binding Path="ItemsSource" ElementName="treeView1"/>
                    <Binding Path="Name"/>
                </MultiBinding>
            </HierarchicalDataTemplate.ItemsSource>
            <TextBlock FontWeight="Bold" Text="{Binding Name}" />
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>