我使用来自ObjectResult
方法的实体框架Execute
将数据绑定到WPF控件,如下所示:
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();
}
}
}
该示例来自MSDN
它工作正常,但如何刷新绑定?以编程方式还是在数据库更改时?
答案 0 :(得分:0)
应该从此事件中分离SalesOrdersForm_Loaded代码。
将此代码放在一个函数中。并在表单加载中调用它。现在您可以根据需要调用此函数。
我希望这是有道理的。
修改
您可以根据按钮点击/定时器或任何事件调用此功能,具体取决于您更新绑定的要求