C# 预订(酒店)数据网格中的即将预订绿色,旧预订红色

C# 预订(酒店)数据网格中的即将预订绿色,旧预订红色,c#,wpf,datagrid,C#,Wpf,Datagrid,我一直在网上搜索标题中的选项,我看到了一个示例,第一行是绿色,其他行是红色,这是结论: <DataGrid> <DataGrid.ItemContainerStyle> <Style TargetType="DataGridRow"> <Setter Property="Background" Value="LightBlue"/> <Style.Triggers> <

我一直在网上搜索标题中的选项,我看到了一个示例,第一行是绿色,其他行是红色,这是结论:

<DataGrid>
<DataGrid.ItemContainerStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Background" Value="LightBlue"/>
        <Style.Triggers>
            <DataTrigger
              Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}"
              Value="{x:Null}">
                <Setter Property="Background" Value="Green"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.ItemContainerStyle>
我试图通过编辑到datetime来改变它,但我不是专家。我有这个

 `<DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
              <Setter Property="Background" Value="Red" />
                   <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}" Value="{x:Static sys:DateTime.Now}">
                            <Setter Property="Background" Value="Green"/>
                        </DataTrigger>
                    </Style.Triggers>
             </Style> 
`

有人能帮我吗?
提前谢谢

下面的XAML假定它绑定到的对象是YourItemSource,并且它有一个名为Reservation的属性。如果Reservation属性的值为Old,则该行将变为红色。如果预定的价值即将到来,它将变为绿色


调整名称,使其与对象、特性名称和值内联。此外,基于这些值,可以指示您想要的颜色。

一种方法是使用转换器:

public class StringToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value.ToString() == "old")
        {
            var color = (Color)ColorConverter.ConvertFromString("Red");
            return color;
        }
        else if (value.ToString() == "Upcoming")
        {
            var color = (Color)ColorConverter.ConvertFromString("Green");
            return color;
        }


        return (Color)ColorConverter.ConvertFromString("Black");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后在XAML中:

<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWpf"
        xmlns:conv="clr-namespace:TestWpf.StringToColorConverter"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <conv:StringToColorConverter x:Key="colorConverter" />
    </Window.Resources>
    <Grid>
        <DataGrid>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background">
                        <Setter.Value>
                            <Binding Path="Reservation" Converter="{StaticResource StringToColorConverter}"/>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid> 
    </Grid>
</Window>

可能需要使用某种转换器来实现实际逻辑;然后数据在转换器输出上触发。在XAML中完成整个事情不会有好的结局
<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWpf"
        xmlns:conv="clr-namespace:TestWpf.StringToColorConverter"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <conv:StringToColorConverter x:Key="colorConverter" />
    </Window.Resources>
    <Grid>
        <DataGrid>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background">
                        <Setter.Value>
                            <Binding Path="Reservation" Converter="{StaticResource StringToColorConverter}"/>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid> 
    </Grid>
</Window>