【问题标题】:Silverlight DataServices add items dynamically to the collectionSilverlight DataServices 将项目动态添加到集合中
【发布时间】:2013-09-04 18:39:58
【问题描述】:

我有一个使用 WCF DataService 的 Datagrid。 以下是我正在使用的代码;

public partial class MainPage : UserControl
{
    static ServiceReference1.SampleDbEntities entities = new ServiceReference1.SampleDbEntities(new Uri("http://localhost:1324/WcfDataService1.svc/"));
    static DataServiceQuery<ServiceReference1.Book> query = entities.Books.IncludeTotalCount();
    static WcfDataServicesDataSourceProvider<ServiceReference1.Book> context = new WcfDataServicesDataSourceProvider<ServiceReference1.Book>(query, entities);

    public MainPage()
    {
        Xceed.Silverlight.DataGrid.Licenser.LicenseKey = "****-A7K1K-****-BBUA";
        this.DataContext = context;
        InitializeComponent();
    }
}

现在我需要将新添加的项目添加到网格而不刷新它。我已经看到我可以为此使用“context.NotifyItemsAdded”。

如何获取新添加的项目并将它们插入到网格中? 我可以枚举当前加载的项目吗?

【问题讨论】:

    标签: c# wcf silverlight xceed-datagrid


    【解决方案1】:

    最好使用ObservableCollection并将其设置为DataContext,新添加的项目将自动插入到Grid中。

    我不知道 ObservableCollection 的任何图形教程。 ObservableCollection 类是一个集合类型(如 List),这意味着它拥有给定类型 T 的对象。ObservableCollection 的特别之处在于它“告诉”观察者new 对象被添加或对象被删除。这对于使用 WPF 实现的 UI 特别有用,因为本质上,当将对象添加到可观察集合或从可观察集合中删除时,UI 会自动更新。发生这种情况是因为,当绑定到可观察集合时,WPF 会自动将事件处理程序添加到 ObservableCollecion 的 CollectionChanged 事件。

    找到一个有用的教程here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多