在列表框中显示项目集合的集合。 [可能的HierachicalDataTemplate]

时间:2013-12-01 17:12:18

标签: c# wpf listbox

我将Data类绑定到DataContext。 数据类有父母的集合,每个父母都有儿童的集合。

问题很容易描述如何在ListBox中显示所有Childrens,而不显示Parent?

这是我的示例课程:

 public class Data : ObservableCollection<Parent>
    {
        public Data()
        {
            Parent p1 = new Parent();
            p1.ParenName = "A1";
            p1.Childrens.Add(new Children
            {
                ChildrenName = "Children-1",
                ChildrenCode = "A1-1"
            });
            p1.Childrens.Add(new Children
            {
                ChildrenName = "Children-2",
                ChildrenCode = "A1-2"
            });
            Parent p2 = new Parent();
            p2.ParenName = "A2";
            p2.Childrens.Add(new Children
            {
                ChildrenName = "Children-3",
                ChildrenCode = "A2-1"
            });
            p2.Childrens.Add(new Children
            {
                ChildrenName = "Children-4",
                ChildrenCode = "A2-2"
            });

            this.Add(p1);
            this.Add(p2);
        }
    }

    public class Parent
    {
        public Parent()
        {
            this._childrens = new ObservableCollection<Children>();
        }

        string _parenName;

        public string ParenName
        {
            get { return _parenName; }
            set { _parenName = value; }
        }

        ObservableCollection<Children> _childrens;

        public ObservableCollection<Children> Childrens
        {
            get { return _childrens; }
            set { _childrens = value; }
        }
    }

    public class Children
    {
        string _childrenName;

        public string ChildrenName
        {
            get { return _childrenName; }
            set { _childrenName = value; }
        }

        string _childrenCode;

        public string ChildrenCode
        {
            get { return _childrenCode; }
            set { _childrenCode = value; }
        }
    }

和XAML:

<Window x:Class="ListBoxSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="ListBoxSample">
    <Window.Resources>
        <DataTemplate x:Key="parent">
            <TextBlock Text="{Binding Path=ParentName}"></TextBlock>
        </DataTemplate>
    </Window.Resources>
    <Grid>
            <ListBox ItemsSource="{Binding Source=Parent}" ItemTemplate="{StaticResource parent}"></ListBox>
    </Grid>
</Window>

有人可以告诉我如何使用HierahicalDataTemplate吗?

1 个答案:

答案 0 :(得分:0)

HierachicalDataTemplate适用于TreeView。如果要在ListBox中显示它,请创建一个wrapper property containing flat list for children并使用该包装器属性绑定ListBox。

或者

使用IValueConverter展平您的收藏集,该收藏集将返回子列表。

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                           System.Globalization.CultureInfo culture)
    {
        var parentList = value as ObservableCollection<Parent>;
        var childCollection = new ObservableCollection<Children>();
        if (parentList != null)
        {
            childCollection = new ObservableCollection<Children>
                                 (parentList.SelectMany(p => p.Childrens));
        }
        return childCollection;
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
                                System.Globalization.CultureInfo culture)
    {
       return Binding.DoNothing;
    }
}

XAML -

创建转换器实例并使用它将其与ItemsSource绑定 -

<ListBox ItemsSource="{Binding Source=Parent,
                         Converter={StaticResource MyConverter}}"
         DisplayMemberPath="ChildrenName"/>