XAML中的Case语句与DataTemplate

时间:2013-05-23 00:32:30

标签: wpf xaml datatemplate

我想实现一个列表框,根据绑定数据中的属性显示不同的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为列表框实现不同的数据模板?

1 个答案:

答案 0 :(得分: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);
    }
}