Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 带有DataTemplate的XAML中的案例说明 我想实现一个列表框,根据绑定数据中的属性显示不同的DataTemplate。_Wpf_Xaml_Datatemplate - Fatal编程技术网

Wpf 带有DataTemplate的XAML中的案例说明 我想实现一个列表框,根据绑定数据中的属性显示不同的DataTemplate。

Wpf 带有DataTemplate的XAML中的案例说明 我想实现一个列表框,根据绑定数据中的属性显示不同的DataTemplate。,wpf,xaml,datatemplate,Wpf,Xaml,Datatemplate,我有一个名为Notification的类,它可以保存所有类型的通知 完全依赖于它所持有的数据,而不是为每个项目单独设置类 public class Notification : Interfaces.IListable { Cache.NotificationType _type; public Cache.NotificationType Type { get {return _type;} set {

我有一个名为
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
属性的不同,我希望有一个不同的数据模板

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

如何根据
通知为列表框实现不同的数据模板。FriendlyType
我的列表框正在显示?

您正在寻找:


XAML:

您正在寻找一个:


XAML:


我给了你一个问题的答案,但作为旁注,当打开枚举时,你应该始终为
默认值:
抛出
ArgumentOutOfRangeException
,以防万一它超出范围……下面的m-y答案应该有效。另一种解决方案是创建派生的通知类,例如DocumentAnnounceNotification。。。DocumentCommentNotification等。然后您可以像m-y的回答中那样创建数据模板,但将每个数据模板设置为针对不同的通知派生类。这里有一个链接到我以前的一个答案,这个答案在概念上非常相似。查看传入和传出消息类以及datatemplates如何使用它们。我为您的问题提供了答案,但作为补充说明,在打开枚举时,您应该始终为
默认值:
抛出
ArgumentOutOfRangeException
,以防超出范围…下面m-y的答案应该有效。另一种解决方案是创建派生的通知类,例如DocumentAnnounceNotification。。。DocumentCommentNotification等。然后您可以像m-y的回答中那样创建数据模板,但将每个数据模板设置为针对不同的通知派生类。这里有一个链接到我以前的一个答案,这个答案在概念上非常相似。看看传入和传出消息类,以及datatemplates是如何使用它们的。
<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}"
  ... />
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);
    }
}