为什么 Silverlight 绑定不起作用?

发布于 2025-01-04 00:20:12 字数 587 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2025-01-11 00:20:12

由于您的 GoogleBlogsResults 属性是

    List<Blog> 

向其添加项目,因此不会触发绑定引擎,因为调用时对象引用没有更改

    NotifyPropertyChanged("GoogleBlogsResults");

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

    ObservableCollection<Blog>

以下情况下自动触发绑定引擎集合中的项目会被修改,或者生成一个新

    List<Blog> 

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

希望这有帮助。

As your GoogleBlogsResults property is a

    List<Blog> 

adding items to it will not trigger the binding engine to fire as the object reference has not changed when you call

    NotifyPropertyChanged("GoogleBlogsResults");

You can solve this by following the solution described by T.Ho by using

    ObservableCollection<Blog>

which triggers the binding engine automatically when items within the collection are modified or alternatively by generating a new

    List<Blog> 

object (which merges the new and old items) and setting the GoogleBlogsResults property to the new list.

Hope this helps.

初吻给了烟 2025-01-11 00:20:12

这个教程可能对你有帮助,

我想你需要在 XAML 中绑定源

DataContext="{Binding Source={StaticResource CustomerContainerObject}}"

<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}"></TextBlock>
    </DataTemplate>
</ListBox.ItemTemplate>                        
  var employees = new List<Employee>() 
{
new Employee { Name = "Scott" },
new Employee { Name = "Poonam"},
new Employee { Name = "Paul" }
};
this.DataContext = employees;

希望有帮助。

this Tutorial may help you

i think you need to binding source in XAML

DataContext="{Binding Source={StaticResource CustomerContainerObject}}"

or

<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Name}"></TextBlock>
    </DataTemplate>
</ListBox.ItemTemplate>                        
  var employees = new List<Employee>() 
{
new Employee { Name = "Scott" },
new Employee { Name = "Poonam"},
new Employee { Name = "Paul" }
};
this.DataContext = employees;

wish that helps.

圈圈圆圆圈圈 2025-01-11 00:20:12

您可能希望使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文