Syncfusion SfDataGrid基于值改单元格前景色异常排查
问题描述
尝试通过转换器根据ItemsSource中对象的DateTime值修改Syncfusion SfDataGrid单元格的前景色,但运行程序时抛出异常:'GridCell' TargetType与元素'GridCell'类型不匹配,该异常逻辑矛盾,请求排查问题。
相关代码
数据模型(ItemsSource来源)
public class RelationalTable { public int ClaimNumber { get; set; } public DateTime PlanPosted { get; set; } public DateTime PlanDue { get; set; } public int PlanVersion { get; set; } public string Department { get; set; } }
SfDataGrid XAML标记
<UserControl.Resources> <!-- converter is defined in the UserControl header xmlns:converter=<assembly holding the converter code> --> <converter:StyleConverterByDate x:Key="ConverterResult" /> </UserControl.Resources> <sf:SfDataGrid x:Name="SfGrid" ItemsSource="{Binding ClaimList}" AutoGenerateColumns="False" IsReadOnly="True" AllowSorting="True" AllowFiltering="True" FontFamily="{StaticResource AppFont}" AllowEditing="False" AllowTriStateSorting="True" ColumnSizer="AutoWithLastColumnFill" RowHeight="32"> <i:Interaction.Behaviors> <loc:SfDataGridBehavior /> </i:Interaction.Behaviors> <sf:SfDataGrid.Columns> <sf:GridTextColumn HeaderText="Claim Number" HorizontalHeaderContentAlignment="Left" MappingName="ClaimNumber"> </sf:GridTextColumn> <sf:GridDateTimeColumn CustomPattern="MM/dd/yyyy" HorizontalHeaderContentAlignment="Left" HeaderText="Date Posted" MappingName="PlanPosted" Pattern="CustomPattern" /> <sf:GridDateTimeColumn CustomPattern="MM/dd/yyyy" HorizontalHeaderContentAlignment="Left" HeaderText="Date Due" MappingName="PlanDue" Pattern="CustomPattern"> <sf:GridDateTimeColumn.CellStyle> <Style TargetType="sf:GridCell"> <Setter Property="Foreground" Value="{Binding PlanDue, Converter={StaticResource ConverterResult}}" /> </Style> </sf:GridDateTimeColumn.CellStyle> </sf:GridDateTimeColumn> <sf:GridTextColumn HeaderText="Department" HorizontalHeaderContentAlignment="Left" MappingName="Department" /> </sf:SfDataGrid.Columns> </sf:SfDataGrid>
转换器代码
public class StyleConverterByDate : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DateTime _value = (DateTime)value; DateTime _today = DateTime.Today; if (_value.Date >= _today.Date) return new SolidColorBrush(Colors.Red); if (_value.Date > _today.Date.AddDays(-10) && _value.Date < _today.Date) return new SolidColorBrush(Colors.Yellow); return new SolidColorBrush(Colors.Black); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return new SolidColorBrush(Colors.Black); } }
问题排查与解决
这个异常看似矛盾,实际是命名空间冲突或Style定义方式错误导致的,以下是具体修复步骤:
1. 确认GridCell命名空间一致性
检查XAML头部的xmlns:sf命名空间是否指向正确的Syncfusion程序集(通常为xmlns:sf="clr-namespace:Syncfusion.UI.Xaml.Grid;assembly=Syncfusion.SfGrid.WPF"),避免项目中存在自定义GridCell类或其他程序集同名类造成混淆。
2. 修改CellStyle定义方式
针对Syncfusion的GridDateTimeColumn,直接设置CellStyle时需确保样式继承默认单元格样式:
<sf:GridDateTimeColumn.CellStyle> <Style TargetType="sf:GridCell" BasedOn="{StaticResource {x:Type sf:GridCell}}"> <Setter Property="Foreground" Value="{Binding PlanDue, Converter={StaticResource ConverterResult}}" /> </Style> </sf:GridDateTimeColumn.CellStyle>
如果上述方法无效,可改用CellTemplate替代CellStyle,彻底规避样式冲突:
<sf:GridDateTimeColumn CustomPattern="MM/dd/yyyy" HorizontalHeaderContentAlignment="Left" HeaderText="Date Due" MappingName="PlanDue" Pattern="CustomPattern"> <sf:GridDateTimeColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding PlanDue, StringFormat='MM/dd/yyyy'}" Foreground="{Binding PlanDue, Converter={StaticResource ConverterResult}}" VerticalAlignment="Center" /> </DataTemplate> </sf:GridDateTimeColumn.CellTemplate> </sf:GridDateTimeColumn>
3. 优化转换器健壮性
虽然不是异常直接原因,建议在转换器中添加空值判断避免潜在崩溃:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is not DateTime dateValue) return new SolidColorBrush(Colors.Black); DateTime today = DateTime.Today; if (dateValue.Date >= today.Date) return new SolidColorBrush(Colors.Red); if (dateValue.Date > today.AddDays(-10) && dateValue.Date < today.Date) return new SolidColorBrush(Colors.Yellow); return new SolidColorBrush(Colors.Black); }
内容的提问来源于stack exchange,提问作者Tim Jones
相关产品推荐
相关产品推荐

