0

我需要使用一個ObservableCollection並且只能使用一個類。這是我的代碼。出於某種原因,我無法讓TreeView使用Observable Collection進行填充。任何幫助,將不勝感激。在WPF中使用HierarchicalDataTemplate和Observable

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> 

類:

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 
    } 
} 
+0

ValidationItem是否真的是私人的? – Phil

回答

1

UPDATE:OK,很多會在這裏,所以花了一段時間來真正瞭解。兩件事情。

更改你的默認構造函數模型

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")); 
    } 

另一種是改變你的「子項目」屬性。 HierarchicalDataTemplate期望ItemsSource是一個IEnumerable。所以改變屬性是

public IEnumerable<SubItem> subItems 

即使你只有一個,使它成爲一個IEnumerable。你還需要改變你的HierarchicalDataTemplates到

<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> 

它還有助於調試應用程序並查看輸出窗口。如果有任何綁定問題,它會告訴你。就像「錯誤綁定,找不到屬性'消息'」。

+0

不會。它的行爲就像Observable Collection中沒有任何東西......它編譯並運行良好,沒有任何錯誤。我認爲在我將可觀察的集合添加到內容時可能存在問題。 – JLott

+0

一旦我真正理解你在做什麼,對我的文章做出了重大改變。 –

+0

感謝您的幫助。我最終採取了一個全新的方向,並將可觀察集合添加到列表中。 – JLott

相關問題