ListView与observablecollection的双向绑定不会自动更新

发布于 2024-12-09 15:09:37 字数 279 浏览 0 评论 0原文

public ObservableCollection<App> Apps { get; set; }

public MainWindow()
{
    Apps = LoadApps();
    listview.ItemsSource = Apps;
}

public void AnotherMethod()
{
    Apps = LoadApps();
}

当分配给 ItemsSource 时它可以工作,但我需要每次应用程序内容更改时更新 ListView。

public ObservableCollection<App> Apps { get; set; }

public MainWindow()
{
    Apps = LoadApps();
    listview.ItemsSource = Apps;
}

public void AnotherMethod()
{
    Apps = LoadApps();
}

It works when assigned to ItemsSource but I need the ListView to be updated every time the Apps content is changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

私藏温柔 2024-12-16 15:09:37

ObservableCollection 仅在更新集合上的项目时才更新 ListView 项目。
您可以执行类似操作

public void AnotherMethod()
{
    var apps = LoadApps();
    Apps.Clear();
    Apps.AddRange(apps);
}

或将 Apps 定义为依赖项属性并将其绑定到列表视图 ItemSource:

public static readonly DependencyProperty AppsProperty =
            DependencyProperty.Register("Apps", typeof (ObservableCollection<App>), typeof (YourClass), new PropertyMetadata(default(ObservableCollection<App>)));

public ObservableCollection<App> Apps
{
    get { return (ObservableCollection<App>) GetValue(AppsProperty); }
    set { SetValue(AppsProperty, value); }
}

public MainWindow()
{
    Apps = LoadApps();
    listview.SetBinding(ListView.ItemsSourceProperty, new Binding("Apps"){Source = this})
}

ObservableCollection only update ListView items when items on collection was updated.
you can do something like

public void AnotherMethod()
{
    var apps = LoadApps();
    Apps.Clear();
    Apps.AddRange(apps);
}

or define Apps as a Dependency Property and bind it to list view ItemSource:

public static readonly DependencyProperty AppsProperty =
            DependencyProperty.Register("Apps", typeof (ObservableCollection<App>), typeof (YourClass), new PropertyMetadata(default(ObservableCollection<App>)));

public ObservableCollection<App> Apps
{
    get { return (ObservableCollection<App>) GetValue(AppsProperty); }
    set { SetValue(AppsProperty, value); }
}

public MainWindow()
{
    Apps = LoadApps();
    listview.SetBinding(ListView.ItemsSourceProperty, new Binding("Apps"){Source = this})
}
紫南 2024-12-16 15:09:37

“应用程序内容”是什么意思?如果将整个引用替换为新集合,则需要实施 < a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" rel="nofollow">MainWindow 中的INotifyPropertyChanged。如果项目内部发生变化并且您希望在 ListView 中进行更新,则需要在 App 中实现所述接口。

(请注意第一种可能性:通常您不会允许通过创建私有只读字段来替换引用,显然公开它的公共属性只会有一个 getter。)

What do you mean by "Apps content"? If you replace the whole reference with a new collection you need to implement INotifyPropertyChanged in MainWindow. If the items change internally and you want updates in the ListView you need to implement said interface in App.

(Note about the first possibility: Often you would not allow the replacement of the reference by creating a private readonly field, obviously the public property exposing it would only have a getter.)

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