为什么 Silverlight 绑定不起作用?
我有 2 个由数据填充的列表框,如下所示:
GoogleNewsResults.Add(
new News()
{
Content = "News Content", Title = "News Title"
});
NotifyPropertyChanged("GoogleNewsResults");
GoogleBlogsResults.Add(
new Blog()
{
Content = "Blog Content", Title = "Blog Title"
});
NotifyPropertyChanged("GoogleBlogsResults");
但它不会更新博客结果列表框。你知道为什么吗? XAML 具有这种类型的绑定:
<sllb:ListBox x:Name="GoogleBlogsList" ItemsSource="{Binding GoogleBlogsResults, Mode=TwoWay}" />
I have 2 listboxes populated by data like so:
GoogleNewsResults.Add(
new News()
{
Content = "News Content", Title = "News Title"
});
NotifyPropertyChanged("GoogleNewsResults");
GoogleBlogsResults.Add(
new Blog()
{
Content = "Blog Content", Title = "Blog Title"
});
NotifyPropertyChanged("GoogleBlogsResults");
But it doesnt update Blog Results listbox. Do you have any idea why?
The XAML has this type of binding:
<sllb:ListBox x:Name="GoogleBlogsList" ItemsSource="{Binding GoogleBlogsResults, Mode=TwoWay}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您的 GoogleBlogsResults 属性是
向其添加项目,因此不会触发绑定引擎,因为调用时对象引用没有更改
您可以按照 T.Ho 描述的解决方案来解决此问题,方法是在
以下情况下自动触发绑定引擎集合中的项目会被修改,或者生成一个新
对象(合并新旧项目)并将 GoogleBlogsResults 属性设置为新列表。
希望这有帮助。
As your GoogleBlogsResults property is a
adding items to it will not trigger the binding engine to fire as the object reference has not changed when you call
You can solve this by following the solution described by T.Ho by using
which triggers the binding engine automatically when items within the collection are modified or alternatively by generating a new
object (which merges the new and old items) and setting the GoogleBlogsResults property to the new list.
Hope this helps.
这个教程可能对你有帮助,
我想你需要在 XAML 中绑定源
或
希望有帮助。
this Tutorial may help you
i think you need to binding source in XAML
or
wish that helps.
您可能希望使用
ObservableCollection
来代替您的列表项,而不是自己实现 INotifyProperityChanged 接口。 ObservableCollection 是一个实现了 INotifyProperityChanged 的集合类,因此您不必手动调用“NotifyPropertyChanged”来更新绑定。确保您拥有正确的 DataContext。如果您不使用 MVVM 设计模式,您可能需要研究一下。
Instead of implement INotifyProperityChanged interface yourself, you might want to use
ObservableCollection<T>
instead for your list items. ObservableCollection is a collection class with INotifyProperityChanged implemented, so you don't have to manually call "NotifyPropertyChanged" for the binding to update.Make sure you have the right DataContext. If you're not using MVVM design pattern, you might want to look into that.