PropertyChanged与TemplateSelector和LongListSelector问题中的对象列表分组(PropertyChanged with TemplateSelector an

编程入门 行业动态 更新时间:2024-10-24 12:31:15
PropertyChanged与TemplateSelector和LongListSelector问题中的对象列表分组(PropertyChanged with TemplateSelector and grouped List of objects in LongListSelector issue)

我正在使用MVVM模式为windows phone 8编写聊天应用程序。 我的问题是通知LongListSelector ObservableCollection项的组正在改变。 好的一步一步:

分组的联系人列表是一个对象

public ObservableCollection<Group<ContactModel>> GroupedContacts

哪个组是:

public class Group<T> : ObservableCollection<T> { public Group(string name, IEnumerable<T> items) { this.Key = name; foreach (T item in items) { this.Add(item); } } public string Key { get; set; } }

然后我向该对象添加两种格式:

private void CreateGroups() { var group = new Group<ContactModel>("Online", new ObservableCollection<ContactModel>()); GroupedContacts.Add(group); group = new Group<ContactModel>("Offline", new ObservableCollection<ContactModel>()); GroupedContacts.Add(group); }

在向每个组添加一些ContactModel对象后,我将它绑定到我的LongListSelector当ContactModel属性Online正在改变时,我将该对象从一个组移动到另一个组 - 这没有问题。 问题是我的TemplateSelector没有改变ContentTemplate 。

TemplateSelector:

public class AvailabilityTemplateSelector : ContentControl { public DataTemplate OnlineTemplate { get; set; } public DataTemplate OfflineTemplate { get; set; } protected override void OnContentChanged(object oldContent, object newContent) { base.OnContentChanged(oldContent, newContent); var contact = newContent as ContactModel; if (contact.Online) { ContentTemplate = OnlineTemplate; } else { ContentTemplate = OfflineTemplate; } } }

我认为这里的问题是我将对象从一个组移动到另一个组。 GroupedContacts属性并不意味着该属性已更改。 属性GroupedContacts中的ObservableCollection执行它,但它不是我的LongListSelector的Content 。 知道如何从ObservableCollection “告诉”我的GroupedContacts属性设置了更改然后从GroupedContacts到我的LongListSelector

I am writing chat application for windows phone 8 using MVVM pattern. My problem is to notify the LongListSelector where group of ObservableCollection items is changing. Ok step by step:

The grouped contacts list is an object

public ObservableCollection<Group<ContactModel>> GroupedContacts

Where group is:

public class Group<T> : ObservableCollection<T> { public Group(string name, IEnumerable<T> items) { this.Key = name; foreach (T item in items) { this.Add(item); } } public string Key { get; set; } }

Then I am adding two kind of grups to that object:

private void CreateGroups() { var group = new Group<ContactModel>("Online", new ObservableCollection<ContactModel>()); GroupedContacts.Add(group); group = new Group<ContactModel>("Offline", new ObservableCollection<ContactModel>()); GroupedContacts.Add(group); }

After adding some ContactModel objects to each of groups I am binding it to my LongListSelector When property Online of ContactModel is changing I am moving that object from one group to another - that works without problem. Problem is my TemplateSelector is not changing the ContentTemplate.

TemplateSelector:

public class AvailabilityTemplateSelector : ContentControl { public DataTemplate OnlineTemplate { get; set; } public DataTemplate OfflineTemplate { get; set; } protected override void OnContentChanged(object oldContent, object newContent) { base.OnContentChanged(oldContent, newContent); var contact = newContent as ContactModel; if (contact.Online) { ContentTemplate = OnlineTemplate; } else { ContentTemplate = OfflineTemplate; } } }

I think the problem here is where I am moving the object from one group to another. The GroupedContacts property is not notyfing that property has changed. The ObservableCollection that is in property GroupedContacts does it but it's not the Content of my LongListSelector. Any idea how to "tell" from ObservableCollection to my GroupedContacts property that changes were set and then from GroupedContacts to my LongListSelector

最满意答案

你的例子中有一些错误。

您的ContentControl知道您的模型。 这是WPF的禁忌!

如果你想听内容更改,你不应该编写自定义控件,而应该使用DataTemplateSelector。

http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector(v=vs.110).aspx

I've made a Converter instead of TemplateSelector and everything is working fine at this moment. I needed to implement INotifyPropertyChanged to my ContactModel class to make sure that changes to layout will be "live".

更多推荐

GroupedContacts,LongListSelector,group,public,ObservableCollection,电脑培训,计算机培训,IT

本文发布于:2023-07-26 02:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269930.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   列表   TemplateSelector   LongListSelector   PropertyChanged

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!