我打开了一个表单,然后我去了表并更新了一个记录。然后我返回并刷新表单,但该记录未更新。
表单中有DataGrid
,绑定到ObservableCollection
。在刷新时,我使用以下命令将数据库中的记录添加到此ObservableCollection
:
filteredProductList.Clear();
foreach(Formulation frm in dbContext.Formulations)
{
filterViewedList.Add(frm);
}
PageLoad
事件如下:
private void loadData()
{
try
{
filterViewedList= new ObservableCollection<Formulation>(dbContext.Formulations);
dgRecords1.ItemsSource = filterViewedList;
}
XAML如下:
<DataGrid Height="387" x:Name="dgRecords1" Margin="0,0,64,0"
IsSynchronizedWithCurrentItem="True" Style="{DynamicResource StyleDatagrid}"
ClipboardCopyMode="None" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
SelectionChanged="dgRecords1_SelectionChanged" Grid.Column="1" Grid.Row="3">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="110" Header="Code" Binding="{Binding FrmltnNo, Mode=TwoWay}" IsReadOnly="True" Foreground="Black"/>
<DataGridTextColumn Width="415" Header="Description" Binding="{Binding FrmltnName, Mode=TwoWay}" IsReadOnly="True" Foreground="Black"/>
<DataGridTextColumn Width="*" Header="Status" Binding="{Binding Status, Mode=TwoWay}" IsReadOnly="True" >
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding Converter={StaticResource FGColorKey}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
显示您称为“刷新”的代码。 dgRecords1.ItemsSource = filterViewedList只是一个静态副本。我怀疑UI不知道更新。将filteredProductList公开为公共ObservableCollection FilteredProductList并绑定到FilteredProductList,然后UI将通过ObservableCollection的魔力了解更新。只更新一个而不是重新填充整个列表会更清晰。