我有一个只有两列的自定义样式数据网格。第二列是" DataGridTemplateColumn"使用包含自定义样式文本框和图像的网格。我创建了一个" IsValid" TextBox中的依赖属性,根据绑定对象的属性值更改其背景颜色和边框画笔。图像通过DataTrigger取决于对象的IsValid属性来更改它的来源
每次用户按下Enter键时,文本框基本上会做一些工作,并将对象中的IsValid属性设置为true,false或null(当然,通过对象中的notifypropertychanged,更改会反映在文本框和图像上)。
现在我的问题:当我在数据网格中显示很多行(介于100和1000之间)时,如果禁用RowVirtualization,则垂直滚动变得非常慢,但如果我将其保留,则生成的新行会出现背景颜色错误,旧版本丢失了更改后的布局
这里是DataGrid的xaml
<DataGrid x:Name="dgData"
AutoGenerateColumns="False"
Margin="2"
IsReadOnly="False"
ItemsSource="{Binding}"
SelectionMode="Single"
CanUserAddRows="False"
CanUserReorderColumns="False"
CanUserResizeRows="False"
Style="{StaticResource dDataGrid2}"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
MaxHeight="750"
EnableRowVirtualization="False"
SelectionChanged="dgData_SelectionChanged"
Loaded="dgData_Loaded">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}"
BasedOn="{StaticResource dDataGridRow}">
<EventSetter Event="PreviewMouseLeftButtonUp"
Handler="Row_Click" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="{Binding Source={x:Static fields:FieldNameList.ProductCode}}"
Binding="{Binding Path=ProductCode}"
Width="auto"
MinWidth="160"
IsReadOnly="True" />
<DataGridTextColumn Header="{Binding Source={x:Static fields:FieldNameList.Product}}"
Binding="{Binding Path=ProductDescription}"
Width="*"
IsReadOnly="True" />
<DataGridTemplateColumn Header="Código SNC"
Width="120">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Name="gridSNCCode">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<txt:DTextBox Grid.Column="0"
x:Name="txtSNCCode"
PreviewKeyUp="txtSNCCode_PreviewKeyUp"
TextChanged="DTextBox_TextChanged"
Text="{Binding Path=PHC_CPOC, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
IsValid="{Binding Path=IsPHC_CPOC_Valid, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
NumericOnly="True"
SelectAllOnFocus="true" />
<Image Grid.Column="1"
Name="imgValidation"
Width="16"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="3 0" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsPHC_CPOC_Valid, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
Value="{x:Null}">
<Setter TargetName="imgValidation"
Property="Visibility"
Value="Collapsed" />
<Setter TargetName="imgValidation"
Property="Source"
Value="{x:Null}" />
<Setter TargetName="gridSNCCode"
Property="ToolTip"
Value="" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsPHC_CPOC_Valid, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
Value="True">
<Setter TargetName="imgValidation"
Property="Visibility"
Value="Visible" />
<Setter TargetName="imgValidation"
Property="Source"
Value="pack://application:,,,/SAFT2PHC;component/Resources/Images/Success48.png" />
<Setter TargetName="gridSNCCode"
Property="ToolTip"
Value="Código SNC Válido" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsPHC_CPOC_Valid, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
Value="False">
<Setter TargetName="imgValidation"
Property="Visibility"
Value="Visible" />
<Setter TargetName="imgValidation"
Property="Source"
Value="pack://application:,,,/SAFT2PHC;component/Resources/Images/delete2-48.png" />
<Setter TargetName="gridSNCCode"
Property="ToolTip"
Value="Código SNC Inválido" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
这是c#代码
public ObservableCollections<ProductObj> Products { get; set; }
public MyWindow(List<ProductObj> products)
{
InitializeComponent();
this.Products = new ObservableCollection(products);
this.dgData.ItemsSource = this.Products;
}
如何获得良好的DataGrid性能并且不会丢失单元格更新布局?
我已尝试使用Items.Refresh()但它不起作用......
由于