0

我想实现一个列表框,它根据绑定数据中的属性显示不同的 DataTemplate。

我有一个名为的类Notification,它可以保存不同类型的通知,所有这些都完全依赖于它所保存的数据,而不是为每个项目设置单独的类。

 public class Notification : Interfaces.IListable
{
    Cache.NotificationType _type;
    public Cache.NotificationType Type
    {
        get {return _type;}
        set
        {
            switch (value)
            {
                case Cache.NotificationType.DocumentAnnouncment:
                    this.FriendlyType = "Document Announcment";
                    break;
                case Cache.NotificationType.DocumentComment:
                    this.FriendlyType = "Document Comment";
                    break;
                case Cache.NotificationType.FileTransfer:
                    this.FriendlyType = "File Transfer Progress";
                    break;
                case Cache.NotificationType.GeneralAnnouncment:
                    this.FriendlyType = "Special Announcment";
                    break;
                case Cache.NotificationType.MeetingAnnouncment:
                    this.FriendlyType = "Meeting Announcment";
                    break;
                case Cache.NotificationType.MeetingComment:
                    this.FriendlyType = "Meeting Comment";
                    break;

            }

            _type = value;
        }
    }

这里的主要思想是我想根据FriendlyType属性的不同来使用不同的数据模板。

因此,为了将来的可读性,我将更具体地重申我的问题。

如何根据Notification.FriendlyType显示的列表框为列表框实现不同的数据模板?

4

1 回答 1

5

您正在寻找DataTemplateSelector


XAML:

<DataTemplate x:Key="DocumentAnnouncmentTemplate"> ... </DataTemplate>
<DataTemplate x:Key="DocumentCommentTemplate"> ... </DataTemplate>
<DataTemplate x:Key="FileTransferTemplate"> ... </DataTemplate>
<DataTemplate x:Key="GeneralAnnouncmentTemplate"> ... </DataTemplate>
<DataTemplate x:Key="MeetingAnnouncmentTemplate"> ... </DataTemplate>
<DataTemplate x:Key="MeetingCommentTemplate"> ... </DataTemplate>

<local:FriendlyTypeTemplateSelector x:Key="FriendlyTypeTemplateSelector"
  DocumentAnnouncmentTemplate="{StaticResource DocumentAnnouncmentTemplate}"
  DocumentCommentTemplate="{StaticResource DocumentCommentTemplate}"
  FileTransferTemplate="{StaticResource FileTransferTemplate}"
  GeneralAnnouncmentTemplate="{StaticResource GeneralAnnouncmentTemplate}"
  MeetingAnnouncmentTemplate="{StaticResource MeetingAnnouncmentTemplate}"
  MeetingCommentTemplate="{StaticResource MeetingCommentTemplate}" />

...

<ListBox ItemTemplateSelector="{StaticResource FriendlyTypeTemplateSelector}"
  ... />

C#代码:

public class FriendlyTypeTemplateSelector : DataTemplateSelector
{
    public DataTemplate DocumentAnnouncmentTemplate { get; set; }
    public DataTemplate DocumentCommentTemplate { get; set; }
    public DataTemplate FileTransferTemplate { get; set; }
    public DataTemplate GeneralAnnouncmentTemplate { get; set; }
    public DataTemplate MeetingAnnouncmentTemplate { get; set; }
    public DataTemplate MeetingCommentTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var notification = item as Notification;

        if (notification != null)
        {
            switch (notification.FriendlyType)
            {
                case "Document Announcment":
                    return DocumentAnnouncmentTemplate;
                case "Document Comment":
                    return DocumentCommentTemplate;
                case "File Transfer Progress":
                    return FileTransferTemplate;
                case "Special Announcment":
                    return GeneralAnnouncmentTemplate;
                case "Meeting Announcment":
                    return MeetingAnnouncmentTemplate;
                case "Meeting Comment":
                    return MeetingCommentTemplate;
            }
        }

        return base.SelectTemplate(item, container);
    }
}
于 2013-05-23T00:56:10.810 回答