我正在使用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
发布评论