【发布时间】:2013-12-15 23:08:10
【问题描述】:
我在 xaml 中有以下代码:
<DataGrid Name="DgAlarmsGrid" AutoGenerateColumns="True" Grid.Row="1"
HorizontalAlignment="Stretch" Margin="5,29,5,10"
VerticalAlignment="Stretch" RenderTransformOrigin="0.517,0.861" Grid.RowSpan="2" ItemsSource="{Binding Items}">
<DataGrid.Resources>
<pagingDemo:ValueColorConverter x:Key="Colorconverter"/>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.DisplayIndex}" Value="10">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource Colorconverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
还有这个类:
public class ValueColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var str = value as string;
if (str == null) return null;
int intValue;
if (!int.TryParse(str, out intValue)) return null;
if (intValue < 0) return (MainWindow.AlarmColours[256]);
return (intValue > 255) ? null : (MainWindow.AlarmColours[intValue]);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这会根据单元格的值正确地为单元格的背景着色。但我需要为整行着色,但我无法让它工作。如果我将 CellStyle 更改为 RowStyle 并将 TargetType 更改为 DataGridRow,那么它不会为任何单元格着色。
【问题讨论】: