【问题标题】:Update Listbox after inserting a row into database更新列表框
【发布时间】:2009-04-27 15:02:57
【问题描述】:

我已使用以下代码将 ListBox 绑定到 SQL CE 数据库:

ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:DatabaseWindow}}, Path=Database.Photos1}"

此 ListBox 已正确填充。但是当我尝试向数据库中插入一行时(InsertOnSubmit + SubmitChagnes),ListBox 没有更新!

我试过了:

ListBox.Items.Refresh();

ListBox.GetBindingExpression(ListBox.ItemsSourceProperty).UpdateTarget();

ListBox.ItemsSource = null; 
ListBox.ItemsSource = this.Database.Table;

这些都没有帮助。如何更新 ListBox?!

PS:重新加载数据库后,该行出现在列表框中。 PPS:如果有人知道带有 WPF 的 SQL CE 教程,其中显示了插入到 ListBox 中 - 那也很棒!

【问题讨论】:

    标签: c# .net sql database sql-server-ce


    【解决方案1】:

    确保您分配给 ItemsSource 的数据实现了 INotifyCollectionChanged。 (例如,您可以将您的项目放入 ObservableCollection) 此外,要反映对您的各个项目的更改,您必须实施 INotifyPropertyChanged。

    【讨论】:

    • 是的,我知道这就是 ListBox 没有自动更新的原因。但是如果我可以手动更新它就足够了..
    猜你喜欢
    • 2012-07-15
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多