【问题标题】:Using HierarchicalDataTemplate and Observable in WPF在 WPF 中使用 HierarchicalDataTemplate 和 Observable
【发布时间】:2012-05-22 19:24:26
【问题描述】:

我需要使用 ObservableCollection 并且只需要一个类。这是我的代码。由于某种原因,我无法让 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
    }
}

【问题讨论】:

  • ValidationItem 真的是私有的吗?

标签: wpf xaml treeview observablecollection hierarchicaldatatemplate


【解决方案1】:

更新:好的,这里发生了很多事情,所以需要一段时间才能真正理解。两件事。

将模型的默认构造函数更改为

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

另一个是改变你的“subItem”属性。 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>

它还有助于调试您的应用程序并查看输出窗口。如果有任何绑定问题,它会告诉你。类似于“错误绑定,找不到属性‘消息’”。

【讨论】:

  • 没有。它的行为就像 Observable Collection 中没有任何东西一样......它编译并运行良好,没有任何错误。我认为在我的 observable 集合中添加东西时可能会出现问题。
  • 一旦我真正理解了你在做什么,我就对我的帖子做出了很大的改变。
  • 感谢您的帮助。我最终选择了一个全新的方向并将列表添加到可观察的集合中。
猜你喜欢
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多