0

I need to use an ObservableCollection and only one class. Here is my code. For some reason I cannot get the TreeView to populate with the Observable Collection . Any help would be appreciated.

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>
        <local:ValidationItem x:Key="ValidationMessages" />

        <DataTemplate x:Key="Messages">
            <TextBlock Text="{Binding Messages}" />
        </DataTemplate>

        <HierarchicalDataTemplate x:Key="SubItem" ItemTemplate="{StaticResource Messages}" ItemsSource="{Binding Messages}" >
            <TextBlock Text="{Binding subItem}" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate  ItemTemplate="{StaticResource SubItem}" x:Key="ItemTemplate"
                ItemsSource="{Binding subItem}">
            <TextBlock Text="{Binding item}" FontWeight="Bold" />
        </HierarchicalDataTemplate>

    </UserControl.Resources>

    <Grid>
        <telerik:RadTreeView ItemsSource="{Binding Source={StaticResource ValidationMessages}}"
                ItemTemplate="{StaticResource ItemTemplate}" x:Name="RadTreeView"/> 
    </Grid>
</UserControl>

CLASS:

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

namespace ValidationWPF.DataSources
{
    class ValidationItem : ObservableCollection<ValidationItem>
    {
        public ValidationItem()
        {
        }

        public ValidationItem(Item item, SubItem subItem, string Messages)
        {
            this.item = item;
            this.subItem = subItem;
            this.Message = Messages;
        }

        public string Message { get; set; }

        public SubItem subItem { get; set; }

        public Item item { get; set; }

        public ObservableCollection<ValidationItem> ValidationItems
        {
            get
            {
                Add(new ValidationItem(Item.Customer, SubItem.Name, "Customer Name Cannot be Null"));
                Add(new ValidationItem(Item.Customer, SubItem.Phone, "Phone number cannot be Null"));
                Add(new ValidationItem(Item.Customer, SubItem.Phone, "Phone number must be in the format (###)###-####"));
                Add(new ValidationItem(Item.Customer, SubItem.Address, "Customer Address cannot be Null"));
                return ValidationItems;
            }
        }
    }

    public enum Item
    {
        Customer
    }

    public enum SubItem
    {
        Address,
        Phone,
        Name
    }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
JLott
  • 1,818
  • 3
  • 35
  • 56

1 Answers1

1

UPDATE: OK, lots going on here, so took a while to really understand. Two things.

Change the default constructor of you model to

public ValidationItem()
    {
        Add(new ValidationItem(Item.Customer, SubItem.Name, "Customer Name Cannot be Null"));
        Add(new ValidationItem(Item.Customer, SubItem.Phone, "Phone number cannot be Null"));
        Add(new ValidationItem(Item.Customer, SubItem.Phone, "Phone number must be in the format (###)###-####"));
        Add(new ValidationItem(Item.Customer, SubItem.Address, "Customer Address cannot be Null"));
    }

The other is to change your "subItem" property. the HierarchicalDataTemplate expects the ItemsSource to be an IEnumerable. So change the property to be

public IEnumerable<SubItem> subItems

Even if you only have one, make it an IEnumerable. You will also need to change your HierarchicalDataTemplates to

    <HierarchicalDataTemplate x:Key="SubItem">
        <TextBlock Text="{Binding}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate ItemTemplate="{StaticResource SubItem}" x:Key="ItemTemplate"
            ItemsSource="{Binding subItems}">
        <TextBlock Text="{Binding item}" FontWeight="Bold" />
    </HierarchicalDataTemplate>

It also helps to debug your application and view the Output window. If there are any binding problems it will tell you. Something like "Error binding, could not find property 'Messages'".

Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41
  • No. It acts like there is nothing in the Observable Collection... It compiles and runs fine without any errors. I think there might be a problem when I am adding things to my observable collection. – JLott May 22 '12 at 19:45
  • Made a big change to my post once i truly understood what you are doing. – Shawn Kendrot May 22 '12 at 20:07
  • Thanks for your help. I ended up taking a whole new direction and adding lists to observable collections. – JLott May 23 '12 at 20:47