我希望有人可以帮助我,因为我是一个真正的新手。我有一个绑定到我的webServer上的SQL数据库的WPF应用程序。更新数据库的信息是从自动交易平台完成的。此应用程序旨在实时监控变化。将来自Web服务器的数据绑定到数据网格是没有问题的。然而,我不能为我的生活找出如何实时更新数据网格。我能做的最接近的是添加一个刷新按钮,每次点击它时都会成功更新应用程序。但是,我希望它在数据库发生自动更改时自行更新。有人可以告诉我如何修改我的代码来完成这项工作吗?我已将我的代码放在下面。谢谢!试图通过刷新我的WPF应用程序中的数据集来自动更新我的Datagrid
using System.Windows;
using Microsoft.Windows.Controls.Primitives;
using System.Collections.Generic;
using C1.WPF.DataGrid;
using StylingWPFGrid.ForexDataSetTableAdapters;
using System.Windows.Threading;
using System;
namespace StylingWPFGrid
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class WPFGrid : Window
{
private ForexDataSet _infoDataSet = null;
public ForexDataSet infoDataSet
{
get
{
if (_infoDataSet == null)
{
_infoDataSet = new ForexDataSet();
infoTableAdapter info = new infoTableAdapter();
info.Fill(_infoDataSet.info);
}
return _infoDataSet;
}
}
private ForexDataSet _tradesDataSet = null;
public ForexDataSet tradesDataSet
{
get
{
if (_tradesDataSet == null)
{
_tradesDataSet = new ForexDataSet();
tradesTableAdapter trades = new tradesTableAdapter();
trades.Fill(_tradesDataSet.trades);
}
return _tradesDataSet;
}
}
public WPFGrid()
{
InitializeComponent(); this.AccountsDataGrid.ItemsSource = infoDataSet.info;
InitializeComponent(); this.TradesDataGrid.ItemsSource = tradesDataSet.trades;
}
private void QuitBtn_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void RefreshBtn_Click(object sender, RoutedEventArgs e)
{
infoTableAdapter info = new infoTableAdapter();
info.Fill(_infoDataSet.info);
}
}
}
感谢您的反馈,我修复了Initialize Component并为TwoWay设置了绑定。在我的代码中究竟在哪里放置INotifyPropertyChange存在问题,但我会继续保留它。再次感谢。 – Stan
我更新了代码以向您展示应该放置接口的位置。 – Billy
比利,我试图插入一切,因为你已经显示,并只有一个红色的下划线错误,当我这样做,并在该行的最后一个“e”:PropertyChanged(this,new PropertyChangedEventArgs(e));但是,当我运行应用程序时,我没有错误或警告,应用程序出现并且没有刷新。 – Stan