/ / Color DataGridCell by Cellvalue - c #, wpf, liaison, datagrid, datagridcell

Color DataGridCell par Cellvalue - c #, wpf, liaison, datagrid, datagridcell

"J'ai un WPF DataGrid avec un nombre différent de colonnes. Je souhaite colorier les cellules individuelles en fonction de la valeur. Par exemple: Si la valeur de la cellule est 0, alors le rouge.

Ce sont mes expériences:

<DataGrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="DataGrid"  SelectionUnit="Cell">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<!--experiment 1 -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Value="0">
<Setter Property="Background" Value="LimeGreen"/>
</DataTrigger>
<!--experiment 2 -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Value="0">
<Setter Property="Background" Value="LimeGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>

Réponses:

0 pour la réponse № 1

Utilisez simplement un convertisseur de valeur (avec la valeur de cellule en tant que paramètre), qui renvoie la couleur souhaitée.

<DataGrid>
<DataGridCell Background="{Binding CellValueField, Converter={StaticResource YourDefinedValueToColorConverter}}" />
</DataGrid>

EDIT: enfin réussi à le faire fonctionner.

Convertisseur et définitions de style:

<Window.Resources>
<c:ValueToColorConverter x:Key="ValueToColorConverter"/>
<Style x:Key="CellStyle" TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ValueToColorConverter}}" />
</Style>
</Window.Resources>

Le DataGrid:

<DataGrid HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Loaded="DataGrid_Loaded">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Converter={StaticResource ValueToColorConverter}}" />
</Style>
</DataGrid.CellStyle>
</DataGrid>

Et le convertisseur:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var cell = value as Order;
if (cell != null && cell.Size > 80)
return new SolidColorBrush(Colors.Red);
else return new SolidColorBrush(Colors.Yellow);
}

J'ai utilisé le DataGrid_Loaded méthode pour remplir le DataGrid avec des données aléatoires encapsulées dans un exemple de classe:

class Order
{
public int Size { get; set; }
}

Et le résultat:

entrer la description de l'image ici


0 pour la réponse № 2

Utilisez un convertisseur de valeur, comme ceci:

<DataGridCell Background="{Binding CellValueField, Converter={StaticResource IntegerToColorValueConverter}}" />

Et:

public class IntegerToColorValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
switch ((int)value)
{
case 1: return Color.Red; break;
case 2: return Color.Yellow; break;
Default: return Color.White; break;
}
}

}