我有一个不错的数据网格,并且正在尝试实现“脏标志”系统。每当我手动更改值时,这些标志都会正确更新,但是当我将更改保存到数据库时,这些标志仍会被触发。
以下是与我的DataGrid相关的XAML:
<DataGrid Margin="20" AutoGenerateColumns="False" Name="MasterDataGrid" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="MasterDataGrid_SelectionChanged" AlternationCount="2" DockPanel.Dock="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="NameColumn" IsReadOnly="True" Header="Name" Binding="{Binding Name}" />
<!--<DataGridTextColumn Header="Value" Binding="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=Explicit}">
<DataGridTextColumn.Foreground>
<MultiBinding Converter="{StaticResource MvcForeground}">
<Binding Path="Value" />
<Binding Path="DefaultValue"/>
</MultiBinding>
</DataGridTextColumn.Foreground>
</DataGridTextColumn>-->
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid FocusManager.FocusedElement="{Binding ElementName=textBox1}">
<TextBox Name="textBox1" GotFocus="TextBox1_OnGotFocus" Margin="0" Padding="-2" MaxHeight="29" Text="{Binding Path=TemporaryValueFromUser, Mode=TwoWay, UpdateSourceTrigger=Explicit}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Name="ValueTextBlock" Text="{Binding Path=TemporaryValueFromUser, Mode=TwoWay,UpdateSourceTrigger=Explicit}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource MvcForeground}">
<Binding Path="TemporaryValueFromUser" />
<Binding Path="StoredValue"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn IsReadOnly="True" Header="Stored Value" Binding="{Binding StoredValue, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn IsReadOnly="True" Header="Default Value" Binding="{Binding DefaultValue}"/>
</DataGrid.Columns>
</DataGrid>
此处的主要重点是“ Value”列和“ StoredValue”列。
我的C#位于代码的后面,因此不需要额外的数据上下文。
以下是XAML中使用的多值转换器:
public class MvcForeground : IMultiValueConverter
{
public object Convert(object[] values, System.Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
//var changed = Brushes.Red;
var changed = Brushes.Red;
var unchanged = Brushes.Black;
if (values[0] == null || values[1] == null)
{
return unchanged;
}
if (values.Count() == 2)
{
if (values[0].Equals(values[1]))
return unchanged;
else
return changed;
}
else
return unchanged;
}
public object Reset()
{
return Brushes.Black;
}
public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是我保存更改的方法:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
foreach (CmcdConfigurationParameter cp in MasterDataGrid.ItemsSource)
{
cp.StoredValue = cp.TemporaryValueFromUser;
}
_objDbContext.SaveChanges();
RaiseEvent(new RoutedEventArgs(SaveConfigChangesEvent));
MessageBox.Show("Changes have been saved to database");
}
值转换器正常工作,并且save方法的确确实写入了数据库,唯一的问题是UI事后没有更新,可能是因为当时未调用Convert()
方法。
然后,如何在按下保存按钮后强制UI反映保存的更改?
答案 0 :(得分:1)
由于您不将MVVM模式与PropertyChanged通知一起使用,因此需要将网格重新绑定到ItemsSource数据。
在SaveButton_Click方法的结尾,您需要以下内容:
MasterDataGrid.ItemsSource = MasterDataGrid.ItemsSource.Cast<object>.ToList();