0

ObservableCollection と 1 つのクラスのみを使用する必要があります。これが私のコードです。何らかの理由で、 TreeView に Observable Collection を取り込むことができません。どんな助けでも大歓迎です。

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

1 に答える 1

1

更新: OK、ここで多くのことが起こっているので、本当に理解するのに時間がかかりました. 2つのこと。

モデルのデフォルト コンストラクターを次のように変更します。

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

もう 1 つは、「subItem」プロパティを変更することです。HierarchicalDataTemplate は、ItemsSource が IEnumerable であることを想定しています。したがって、プロパティを次のように変更します

public IEnumerable<SubItem> subItems

1 つしかない場合でも、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>

また、アプリケーションのデバッグや出力ウィンドウの表示にも役立ちます。バインディングの問題がある場合は、それが表示されます。「エラーバインディング、プロパティ「メッセージ」が見つかりませんでした」のようなもの。

于 2012-05-22T19:36:56.883 に答える