我是 WPF 的新手。现在,我正在使用实体框架编写 WPF 项目。我可以在我的 wpf 上正确添加和删除项目;但是,在我添加或删除项目后,我的数据网格仍然没有刷新。即使我使用ssEntities.SaveChanges(); ssEntities.Refresh(RefreshMode.StoreWins, ssEntities.User);
如何使用刷新功能或其他功能自动刷新我的数据网格?
XAML
<Window.Resources>
<CollectionViewSource x:Key="UserViewSource"
d:DesignSource="{d:DesignInstance my:User, CreateList=True}" />
</Window.Resources>
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="537"
HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="12,12,0,0"
Name="UserDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected"
VerticalAlignment="Top" Width="300"
SelectedCellsChanged="UserDataGrid_SelectedCellsChanged">
<DataGrid.Columns>
<DataGridTextColumn x:Name="IDColumn" Binding="{Binding Path=ID}" Header="ID"
Width="80" IsReadOnly="True" />
<DataGridTextColumn x:Name="NameColumn" Binding="{Binding Path=Name}"
Header="Name" Width="80" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
CS
private void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
if (ActionHandler == "Add")
{
User user = new User();
user.ID = ID;
user.Name = Name;
ssEntities.User.AddObject(user);
}
ssEntities.SaveChanges();
ssEntities.Refresh(RefreshMode.StoreWins, ssEntities.User);
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}