我需要在observablecollection中添加一个observablecollection。

时间:2012-05-23 20:58:39

标签: wpf xaml observablecollection hierarchicaldatatemplate radtreeview

这是我的代码以及运行时的输出。我真的很茫然。任何帮助将不胜感激。

VALIDATIONITEM CLASS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace ValidationWPF.DataSources
{
   public class ValidationItem : ObservableCollection<ValidationItem>
    {
        public ValidationItem()
        {
            SubItems = new ObservableCollection<ValidationItem>();


        }


        public ObservableCollection<ValidationMessages> Messages
        {
            get;
            set;
        }

        public string item
        {
            get;
            set;
        }

        public IList<ValidationItem> SubItems
        {
            get;
            set;
        }

        public static IList<ValidationItem> GetItems(string name)
        {
            var Validation = new ObservableCollection<ValidationItem>();


            var item = new ValidationItem();
            item.item = "Customer";


            var subItem = new ValidationItem();
            subItem.item = "Name";
            item.SubItems.Add(subItem);

            var Message = new ValidationItem();
            Message.item = new ObservableCollection<ValidationMessages>().ToString();
            subItem.SubItems.Add(Message);





            Validation.Add(item);

            return Validation;

        }
    }
}

VALIDATIONMESSAGES CLASS(需要在验证项目类中实施)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace ValidationWPF.DataSources
{
    public class ValidationMessages 
    {
        public ValidationMessages(string Message)
        {
            this.Message = Message;
        }

            public string Message
            {
                get;
                set;
            }


    }
}

VALIDATIONUSERCONTROL XAML:

<UserControl x:Class="ValidationWPF.ValidationUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:ValidationWPF.DataSources"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>



    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">

        <telerik:RadTreeView x:Name="radTreeView" Margin="8">
            <telerik:RadTreeView.ItemTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding SubItems}">
                    <TextBlock Text="{Binding item}" />
                </HierarchicalDataTemplate>

            </telerik:RadTreeView.ItemTemplate>
        </telerik:RadTreeView>

    </Grid>
</UserControl>

输出:

Customer
      Name
         Message Below*
    *System.Collections.ObjectModel.ObservableCollection'1[ValidationWPF.DataSources.ValidationMessages]

2 个答案:

答案 0 :(得分:0)

你的输出很好,因为你写道:

Message.item = new ObservableCollection<ValidationMessages>().ToString();

答案 1 :(得分:0)

您的item属性应为ObservableCollection<ValidationMessages>类型 如果没有,那么您应首先遍历item集合并生成一串验证消息,然后再将其分配给Message.item。