我有一个使用数据网格显示 DbSet(实体框架 5)的应用程序。当我使用代码更改某些实体时,最终用户必须执行一些操作(例如重新排序列或编辑单元格)来更新数据网格。
XAML:
<CollectionViewSource x:Key="componentViewSource" d:DesignSource="{d:DesignInstance {x:Type Models:Component}, CreateList=True}"/>
<TabItem Header="Components">
<DataGrid x:Name="componentDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
ItemsSource="{Binding Source={StaticResource componentViewSource}}" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="componentTypeColumn" Binding="{Binding ComponentType}" Header="Component Type" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="descriptionColumn1" Binding="{Binding Description}" Header="Description" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="catalogNumberColumn1" Binding="{Binding CatalogNumber}" Header="Catalog Number" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="commentColumn1" Binding="{Binding Comment}" Header="Comment" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="digiKeyColumn" Binding="{Binding DigiKey}" Header="Digi Key" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="farnellColumn" Binding="{Binding Farnell}" Header="Farnell" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="inStockColumn" Binding="{Binding InStock}" Header="In Stock" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="minimumStockColumn" Binding="{Binding MinimumStock}" Header="Minimum Stock" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="manufacturerColumn" Binding="{Binding Manufacturer}" Header="Manufacturer" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="manufacturerPNColumn" Binding="{Binding ManufacturerPN}" Header="Manufacturer PN" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="mouserColumn" Binding="{Binding Mouser}" Header="Mouser" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="packageColumn" Binding="{Binding Package}" Header="Package" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="priceColumn" Binding="{Binding Price}" Header="Price" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="subTotalColumn" Binding="{Binding SubTotal}" Header="Sub Total" IsReadOnly="True" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="valueColumn" Binding="{Binding Value}" Header="Value" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</TabItem>
C#:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// That's a private field.
componentViewSource = (CollectionViewSource)FindResource("componentViewSource");
// That's the database context.
db = new PotatoContext();
db.Components.Load();
componentViewSource.Source = db.Components.Local;
}
private void Manufacture_Product_Click(object sender, RoutedEventArgs e)
{
var product = productDataGrid.SelectedItem as Product;
if(product == null)
return;
ManufactureProduct d = new ManufactureProduct(product, db);
d.Owner = this;
d.ShowDialog();
if (d.DialogResult ?? false)
{
foreach (var comp in product.Components)
{
comp.Component.InStock -= comp.Quantity * d.NumberOfUnits;
}
db.SaveChanges();
var fileName = string.Format("{0}{1}{2}{3}.txt", ReportGenerator.ProductReportsFolder,
System.IO.Path.DirectorySeparatorChar, product.Name, DateTime.Now.ToString("d.MMM.yyyy HH.mm"));
using(var file = File.Create(fileName))
{
ReportGenerator.GenerateProductionReport(file, product, d.NumberOfUnits);
}
}
}
在第二个事件处理程序内的 foreach 循环中,我更改了一个组件的属性(它已经是 PotatoContext 的一部分),然后我保存了上下文。
当我查看数据网格时,旧值仍然保留,我需要在某个单元格上进入编辑模式以更新该特定单元格,或者单击列标题以更新所有单元格。
这让我很困扰,因为不知情的用户可能不知道这样做。如何从代码中刷新组件?(我试过 Grid.Items.Refresh(); )