Silverlight4 + C#:在 UserControl 中使用 INotifyPropertyChanged 通知另一个 UserControl 未通知
我的项目中有多个用户控件,其中一个从 XML 中检索项目,创建“ClassItem”类型的对象,并应通知其他用户控件有关这些项目的信息。
我已经为我的对象创建了一个类(所有项目都将具有的“模型”):
public class ClassItem
{
public int Id { get; set; }
public string Type { get; set; }
}
我还有另一个类,用于在创建“ClassItem”类型的对象时通知其他用户控件:
public class Class2: INotifyPropertyChanged
{
// Properties
public ObservableCollection<ClassItem> ItemsCollection { get; internal set; }
// Events
public event PropertyChangedEventHandler PropertyChanged;
// Methods
public void ShowItems()
{
ItemsCollection = new ObservableCollection<ClassItem>();
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ItemsCollection"));
}
}
}
数据来自 XML为了创建 ClassItem 类型的对象而进行解析的文件:
void DisplayItems(string xmlContent)
{
XDocument xmlItems = XDocument.Parse(xmlContent);
var items = from item in xmlItems.Descendants("item")
select new ClassItem{
Id = (int)item.Element("id"),
Type = (string)item.Element("type)
};
}
如果我没记错的话,这应该解析 xml 并为它在 XML 中找到的每个项目创建一个 ClassItem 对象。因此,每次创建新的 ClassItem 对象时,都应该为“绑定”到 Class2 中定义的“ItemsCollection”通知的所有 UserControl 触发通知。
然而 Class2 中的代码似乎甚至没有运行:-( 当然也没有通知...
我所做的任何假设是否错误,或者我是否遗漏了某些内容?任何帮助将不胜感激!
谢谢!
I have several User Controls in a project, and one of them retrieves items from an XML, creates objects of the type "ClassItem" and should notify the other UserControl information about those items.
I have created a class for my object (the "model" all items will have):
public class ClassItem
{
public int Id { get; set; }
public string Type { get; set; }
}
I have another class that is used to notify the other User Controls when an object of the type "ClassItem" is created:
public class Class2: INotifyPropertyChanged
{
// Properties
public ObservableCollection<ClassItem> ItemsCollection { get; internal set; }
// Events
public event PropertyChangedEventHandler PropertyChanged;
// Methods
public void ShowItems()
{
ItemsCollection = new ObservableCollection<ClassItem>();
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ItemsCollection"));
}
}
}
The data comes from an XML file that is parsed in order to create the objects of type ClassItem:
void DisplayItems(string xmlContent)
{
XDocument xmlItems = XDocument.Parse(xmlContent);
var items = from item in xmlItems.Descendants("item")
select new ClassItem{
Id = (int)item.Element("id"),
Type = (string)item.Element("type)
};
}
If I'm not mistaken, this is supposed to parse the xml and create a ClassItem object for each item it finds in the XML. Hence, each time a new ClassItem object is created, this should fire the Notifications for all the UserControls that are "bind" to the "ItemsCollection" notifications defined in Class2.
Yet the code in Class2 doesn't even seem to be run :-( and there are no notifications of course...
Am I mistaken in any of the assumptions I've done, or am I missing something? Any help would be appreciated!
Thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
必须访问该属性才能使通知发挥作用。我在代码中没有看到您为“ItemsCollection”设置值的任何地方。
我通常遵循以下模式:
然后更新 ItemsCollection。
The property has to be accessed for the notification to work. I don't see any place in the code where you are setting a value to "ItemsCollection".
I usually follow this pattern:
Then update the ItemsCollection.