I can't believe I can't find an answer to this...
- I've created Entity Framework models generated from DB (SQL Server CE)
I dragged an Entity from Data Sources to my MainWindow, automagically creating this XAML:
<Window.Resources> <CollectionViewSource x:Key="contentItemsViewSource" d:DesignSource="{d:DesignInstance my:ContentItem, CreateList=True}" /> </Window.Resources> <Grid DataContext="{StaticResource contentItemsViewSource}"> <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="Auto" HorizontalAlignment="Stretch" ItemsSource="{Binding}" Margin="0,22" Name="contentItemsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Stretch" Width="Auto" DataContext="{Binding}" IsReadOnly="True" CanUserResizeRows="False"> <DataGrid.Columns> <DataGridTextColumn x:Name="idColumn" Binding="{Binding Path=Id}" Header="Id" Width="SizeToHeader" Visibility="Hidden" /> <DataGridTextColumn x:Name="filePathColumn" Binding="{Binding Path=FilePath}" Header="File Path" Width="SizeToCells" /> <DataGridTextColumn x:Name="fileSizeColumn" Binding="{Binding Path=FileSize}" Header="File Size" Width="SizeToHeader" /> </DataGrid.Columns> </DataGrid> </Grid>
(excuse the XML formatting, the code button doesn't seem to work with it).
I'm loading data in with this code:
foreach (String file in files.FileNames)
{
ContentItem c = new ContentItem() { Id = Guid.NewGuid(), FilePath = file, FileSize = (Int32)finfo.Length };
db.AddToContentItems(c);
}
db.SaveChanges();
This all works, except I need to update the datagrid. The only way I can find to do this is re-using some automagic code created when I bound the entity to the datagrid:
System.Windows.Data.CollectionViewSource contentItemsViewSource = ((System.Windows.Data.Co开发者_JS百科llectionViewSource)(this.FindResource("contentItemsViewSource")));
System.Data.Objects.ObjectQuery<SwordfishWPF.ContentItem> contentItemsQuery = this.GetContentItemsQuery(db);
contentItemsViewSource.Source = contentItemsQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
Surely there's a simpler way? datagrid.Items.Refresh()
doesn't work.
Have you tried this?
CollectionViewSource.GetDefaultView(datagrid.ItemsSource).Refresh();
精彩评论