Как я могу изменить цвет фона строки datagrids, если дата меньше сегодняшней
Я пытаюсь изменить цвет строки, если столбец даты меньше, чем сегодня или истек. Она должна показать, как красный.
Что я уже пробовал:
Я пробовал использовать только XAML, но это просто устанавливает каждую строку в красный цвет.
Мой Код Конвертера
public class CellDateColorConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (values[0] is DateTime && values[1] is DateTime) { DateTime Jobdate = (DateTime)values[0]; DateTime DateToday = (DateTime)values[1]; if (Jobdate.Date < DateToday.Date) { return Color.Red; } } return System.Windows.Data.Binding.DoNothing; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException("CellDateColorConverter is a OneWay converter."); } }
код XAML
<DataGrid xmlns:System="clr-namespace:System;assembly=mscorlib" Cursor="Hand" BorderBrush="#fbfbfb" BorderThickness="1" SelectionUnit="FullRow" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False" IsReadOnly="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="693" Height="520" x:Name="tblunallocatedjobs" ItemsSource="{Binding Path=tblunallocatedjobs}" AutoGenerateColumns="False" Margin="0,20,0,0" MouseDoubleClick="tblunallocatedjobs_MouseDoubleClick" Background="#fbfbfb"> <DataGrid.Resources> <local:CellDateColorConverter x:Key="CellDateColorConverter"/> </DataGrid.Resources> <DataGrid.RowStyle> <Style TargetType="{x:Type DataGridRow}"> <Setter Property="Height" Value="40"></Setter> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Style.Triggers> <DataTrigger Binding="{Binding JobDate.Date}" Value="{x:Static System:DateTime.Today}"> <Setter Property="Background" Value="#00ce3f"/> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowStyle> <DataGrid.Columns> <DataGridTextColumn Header="Job Number" Binding="{Binding ID}"/> <DataGridTextColumn Header="Booked By" Binding="{Binding UserName}"/> <DataGridTextColumn Header="Job" Binding="{Binding JobType}"/> <DataGridTextColumn Header="Date" Binding="{Binding Path=JobDate, StringFormat=\{0:dd/MM/yyyy\}}" x:Name="JobDate"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource CellDateColorConverter}"> <Binding Path="JobDate"/> <Binding Path="DateToday"/> </MultiBinding> </Setter.Value> </Setter> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn Header="Todays Date" Binding="{Binding Path=DateToday, StringFormat=\{0:dd/MM/yyyy\}}" x:Name="DateToday"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource CellDateColorConverter}"> <Binding Path="JobDate"/> <Binding Path="DateToday"/> </MultiBinding> </Setter.Value> </Setter> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn Header="Start Time" Binding="{Binding JobTimeStart}"/> <DataGridTextColumn Header="Finish Time" Binding="{Binding JobTimeFinish}" x:Name="JobTimeFinish"/> <DataGridTextColumn Header="Post Code" Binding="{Binding PostCode}"/> <DataGridTextColumn Header="Price" Binding="{Binding JobPrice}"/> <DataGridTextColumn Header="Job Status" Binding="{Binding Allocation}"/> </DataGrid.Columns> </DataGrid>
с фоновым кодом
private void GetUnallocatedJobs() { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); string query_search = "SELECT * FROM tblJobs WHERE (ID LIKE @ID OR PostCode LIKE @PostCode) AND Allocation='Unallocated' AND JobStatus != 'Cancelled' AND JobDate < @CurrentDate ORDER BY JobDate DESC, PostCode"; SqlCommand cmd = new SqlCommand(query_search, conn); cmd.Parameters.AddWithValue("@CurrentDate", DateTime.Now); cmd.Parameters.AddWithValue("@ID", "%" + Search.Text + "%"); cmd.Parameters.AddWithValue("@PostCode", "%" + Search.Text + "%"); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); dt.Columns.Add("DateToday"); DataRow _ravi = dt.NewRow(); _ravi["DateToday"] = DateTime.Now.ToString("dd/MM/yyyy"); dt.Rows.Add(_ravi); adapter.Fill(dt); tblunallocatedjobs.ItemsSource = dt.DefaultView; } sqlCon.Close(); } }