开发者

Refreshing EF data binding in WPF

开发者 https://www.devze.com 2023-03-07 13:19 出处:网络
I use Entity Framework ObjectResult coming from Execute method to bind data to a WPF control, like here:

I use Entity Framework ObjectResult coming from Execute method to bind data to a WPF control, like here:

using System;
using System.Data;
using System.Data.Objects;
using System.Windows;
using System.Linq;

namespace Microsoft.Samples.Edm
{
 /// <summary>
     /// Interaction logic for SalesOrders.xaml
     /// </summary>
    public partial class SalesOrders : Window
    {
        private AdventureWorksEntities context;
   开发者_开发百科     private int customerId = 277;

    private void SalesOrdersForm_Loaded(object sender, RoutedEventArgs e)
    {
        // Instantiate the ObjectContext.
        context = new AdventureWorksEntities();

        // Define a query that returns orders for a customer.
        // Because lazy loading is on by default, SalesOrderDetails
        // related to a SalesOrderHeader will be loaded when the query
        // is executed.
        var query = from o in context.SalesOrderHeaders
                     where o.CustomerID == customerId
                     select o;

        // Execute the query and bind the result to the OrderItems control.
        this.orderItemsGrid.DataContext = ((ObjectQuery)query).Execute(MergeOption.AppendOnly);
    }

    private void buttonClose_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
    public SalesOrders()
    {
        InitializeComponent();
    }
}
}

The example comes from MSDN

It work fine, but how do I refresh the binding? Either programmatically or when database changes?


The SalesOrdersForm_Loaded code should be seperated from this event.

Place this code in a function. and call it in form load. Now you can call this function on your requirement basis.

I hope it makes sense.

Edit

You can call this function on button click / Timer or any event based on your requirement to update the bindings

0

精彩评论

暂无评论...
验证码 换一张
取 消