我在WPF中创建了一个数据绑定数据网格:'ActionResults',当我运行应用程序时它显示数据库表的内容。使用INotifyPropertyChanged刷新数据绑定数据网格?
I.T部门。将要手动更新SQL Server Management Studio中的'ActionResult'表。如果对表进行了任何更改,则只有在重新启动应用程序后,它们才会显示在datagrid中。我想添加一个“更新”按钮,它将重新刷新数据网格,显示对表格所做的任何更改。
我以前试图做一个黑客的东西,如
actionResultsDataGrid.Items.Refresh();
但决定去与的ObservableCollection。所以我一直在遵循http://www.youtube.com/watch?v=7CKB44Mc4Q0教程,唯一的问题是他手动创建他的记录,我将使用数据库。这里是我到目前为止:
//XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WpfApplication2" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="actionResultsViewSource" d:DesignSource="{d:DesignInstance my:ActionResult, CreateList=True}" />
</Window.Resources>
<Grid DataContext="{StaticResource actionResultsViewSource}">
<DataGrid AutoGenerateColumns="True" EnableRowVirtualization="True" ItemsSource="{Binding}" Name="actionResultsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="0,0,0,85">
<DataGrid.Columns>
<DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id}" Header="Id" Width="SizeToHeader" />
<DataGridTextColumn x:Name="actionColumn" Binding="{Binding Path=Action}" Header="Action" Width="SizeToHeader" />
</DataGrid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="388,253,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
然后我创建了使用INotifyPropertyChanged的更新一个ActionResult类。
public class ActionResults : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private int _Id;
public int Id
{
get { return _Id; }
set
{
_Id = value;
NotifyPropertyChanged("Id");
}
}
private string _Action;
public string Action
{
get { return _Action; }
set
{
_Action = value;
NotifyPropertyChanged("Action");
}
}
在MainWindow.xaml.cs
和我的按钮
private void button1_Click(object sender, RoutedEventArgs e)
{
}
我的问题,从以下该教程他随后创建了另一个类的ActionResults_'和手动添加的记录是,但我现在用的是数据库表。我从这里做什么?请告知我卡住了。可观察收藏的来源在哪里?
谢谢
编辑
private void Load_ActionResult_Table()
{
ActionResultsTable.ActionResultDataSet actionResultDataSet = ((ActionResultsTable.ActionResultDataSet)(this.FindResource("actionResultDataSet")));
// Load data into the table ActionResult. You can modify this code as needed.
ActionResultsTable.ActionResultDataSetTableAdapters.ActionResultTableAdapter actionResultDataSetActionResultTableAdapter = new ActionResultsTable.ActionResultDataSetTableAdapters.ActionResultTableAdapter();
actionResultDataSetActionResultTableAdapter.Fill(actionResultDataSet.ActionResult);
System.Windows.Data.CollectionViewSource actionResultViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("actionResultViewSource")));
actionResultViewSource.View.MoveCurrentToFirst();
}
private void Refresh_Table_Button_Click(object sender, RoutedEventArgs e)
{
//SO DO I NEED TO SAY, SET TABLE TO NULL THEN RELOAD BY USING BELOW FUNCTION?
Load_ActionResult_Table();
}
你为什么不动,从数据库中读取数据的方法的代码,请在开头添加对ObservableCollection.Clear的调用,并在应用程序启动时和按钮点击处理程序中使用该方法? – Markus
AHH我怎么没有想到这个大声笑......感谢回复markus,工作很好。在另一张纸条上,我远离以前的做法吗? – John
INotifyPropertyChanged方式也可能工作,但我认为这个努力会稍微高一点。您需要将ItemsSource绑定到集合属性,就像您为ObservanleCollection所做的一样。不是通知网格有关集合中的更改(ObservableCollection在内部执行此操作),而是创建一个新集合并通知网格该属性已更改。也是一个有效的方法。 – Markus