Skip to main content
 首页 » 编程设计

c#之为什么 Silverlight 绑定(bind)不起作用

2024年02月23日18虾米姐

我有 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 具有这种类型的绑定(bind):

<sllb:ListBox x:Name="GoogleBlogsList" ItemsSource="{Binding GoogleBlogsResults, Mode=TwoWay}" /> 

请您参考如下方法:

因为您的 GoogleBlogsResults 属性是一个

    List<Blog>  

向其添加项目不会触发绑定(bind)引擎,因为在您调用时对象引用未更改

    NotifyPropertyChanged("GoogleBlogsResults"); 

您可以按照 T.Ho 描述的解决方案解决此问题,方法是使用

    ObservableCollection<Blog> 

当集合中的项目被修改或通过生成新的项目时,它会自动触发绑定(bind)引擎

    List<Blog>  

对象(合并新旧项目)并将 GoogleBlogsResults 属性设置为新列表。

希望这对您有所帮助。