0

我看过几篇有类似问题的帖子,我已经尝试全部关注它们。现在终于我在屏幕上获得了所有数据,但不是我想要的方式。但是让我们开始吧。

我有一个 ViewModel,它提供如下数据:

/// <summary>
/// Returns a collection of all the ContinentListViewModel objects
/// </summary>
public static IList<Object> Items
{
    get
    {
        IList<object> childNodes = new List<object>();


        foreach (ContinentViewModel continent in _instance)
        {
            childNodes.Add(continent);
            foreach (CountryViewModel country in continent.CountrytList)
            {
                childNodes.Add(country);
                foreach (BusinessunitViewModel bu in country.BusinessunitList)
                {
                    childNodes.Add(bu);
                }
            }
        }
        return childNodes;
    }
}

这是我的 xaml-TreeView 代码:

            <TreeView Name="tvwBuList" ItemsSource="{Binding BusinessunitList.Items}" HorizontalAlignment="Left" VerticalAlignment="Top" MinHeight="230" MinWidth="265" MaxHeight="230" MaxWidth="265">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type local:ContinentViewModel}" ItemsSource="{Binding}">
                            <TextBlock Text="{Binding Path=ContinentCode}" />
                        </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type local:CountryViewModel}" ItemsSource="{Binding}">
                            <TextBlock Text="{Binding Path=CountryCode}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type local:BusinessunitViewModel}">
                        <RadioButton GroupName="BUGroup" Content="{Binding Path=BuCode}" />
                    </DataTemplate>
                </TreeView.Resources>
                <TreeView.ItemContainerStyle>
                    <Style TargetType="TreeViewItem">
                        <!--Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/-->
                        <Setter Property="IsExpanded" Value="True"/>
                        <Setter Property="Foreground" Value="Yellow"/>
                    </Style>
                </TreeView.ItemContainerStyle>
            </TreeView>

这就是输出的样子:

http://imgh.us/bu_treeview.jpg

我想要的是这样一个完整的结构:

+ AP
|--+ AU
   O-- 164
|--+ CN
   O-- 258
   O-- 277

...

那么我在这里错过了什么?帮助表示赞赏,谢谢!

4

1 回答 1

2

您的 HierarchicalDataTemplates 位于树的同一级别,因为您的 for 循环将它们放入同一个集合中。本质上,您的集合与 XAML 中的绑定不匹配。

我相信要让它工作,你需要为你想要的每个级别单独收集。因此,您将需要一个用于该大陆的集合,而另一个用于该国家/地区的集合。

然后,您需要将资源更新为此:

        <TreeView Name="tvwBuList" ItemsSource="{Binding BusinessunitList.Items}" HorizontalAlignment="Left" VerticalAlignment="Top" MinHeight="230" MinWidth="265" MaxHeight="230" MaxWidth="265">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:ContinentViewModel}" ItemsSource="{Binding Path=Continents}">
                        <TextBlock Text="{Binding Path=ContinentCode}" />
                    </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type local:CountryViewModel}" ItemsSource="{Binding Path=Countries}">
                        <TextBlock Text="{Binding Path=CountryCode}" />
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type local:BusinessunitViewModel}">
                    <RadioButton GroupName="BUGroup" Content="{Binding Path=BuCode}" />
                </DataTemplate>
            </TreeView.Resources>
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <!--Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/-->
                    <Setter Property="IsExpanded" Value="True"/>
                    <Setter Property="Foreground" Value="Yellow"/>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>

那应该给你你想要的。我使用相同的模型来定义带有树视图的消息结构,我们的代码之间的唯一区别是我在自己的集合中拥有每个级别并显式绑定到该集合。

于 2011-08-09T14:32:56.230 回答