Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果值介于a和b之间,则更改DataGridTextColumn的背景_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# 如果值介于a和b之间,则更改DataGridTextColumn的背景

C# 如果值介于a和b之间,则更改DataGridTextColumn的背景,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,因此,我试图根据数据网格中单元格的值更改其背景。因此,如果我想根据静态值更改它,我会使用StyleTrigger,但事实并非如此 例如:如果单元格的值介于80和100之间,我需要绿色背景;如果值介于40和79之间,我需要橙色背景;如果值低于40,我需要红色背景。。。我将如何做到这一点 目前我使用StyleTrigger只是为了测试,所以不介意 以下是我的XAML代码: <DataGrid Background="LightGray" ItemsSource="{Binding Source

因此,我试图根据数据网格中单元格的值更改其背景。因此,如果我想根据静态值更改它,我会使用StyleTrigger,但事实并非如此

例如:如果单元格的值介于80和100之间,我需要绿色背景;如果值介于40和79之间,我需要橙色背景;如果值低于40,我需要红色背景。。。我将如何做到这一点

目前我使用StyleTrigger只是为了测试,所以不介意

以下是我的XAML代码:

<DataGrid Background="LightGray" ItemsSource="{Binding Source={StaticResource Properties}, Path=TableData}"
                  AutoGenerateColumns="False" IsReadOnly="True">
            <DataGrid.Columns >
                <DataGridTextColumn Width="100" Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
                <DataGridTextColumn Width="100" Header="Batterie" Binding="{Binding Battery}" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <Trigger Property="Text" Value="83">
                                    <Setter Property="Background" Value="LightGreen"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Width="100" Header="Current Use" Binding="{Binding CurrentUse}"></DataGridTextColumn>
                <DataGridTextColumn Width="100" Header="Occupancy" Binding="{Binding Occupancy}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>


感谢您的帮助

我通过编写一个从IValueConverter接口实现的转换器解决了这个问题

如果您想在XAML中看到转换器,您需要将其引用到这样的静态资源

<Window.Resources>
    <local:myConverter x:Key="myConverter"/>
</Window.Resources>
我为您的示例创建了一个类(如果我错了,我没有看到您的示例,对不起)

我的项目截图


我通过编写一个转换器解决了这个问题,该转换器通过IValueConverter接口实现

如果您想在XAML中看到转换器,您需要将其引用到这样的静态资源

<Window.Resources>
    <local:myConverter x:Key="myConverter"/>
</Window.Resources>
我为您的示例创建了一个类(如果我错了,我没有看到您的示例,对不起)

我的项目截图

<Setter Property="Background" Value="{Binding Battery, Converter={StaticResource myConverter}}"></Setter>
public class myConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush clr;
        if (int.Parse(value.ToString()) >= 80)
            clr = new SolidColorBrush(Colors.Green);
        else if (int.Parse(value.ToString()) >= 40)
            clr = new SolidColorBrush(Colors.Orange);
        else if (int.Parse(value.ToString()) >= 0)
            clr = new SolidColorBrush(Colors.Red);
        else
            clr = new SolidColorBrush(Colors.White);

        return clr;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
public class Class1
{
    public int ID { get; set; }       
    public string Battery { get; set; }
    public string CurrentUse { get; set; }
    public string Occupancy { get; set; }

    public static List<Class1> myList = new List<Class1>()
    {
        new Class1() {ID = 1, Battery = "70", CurrentUse = "xxxx", Occupancy = "xxxx" },
        new Class1() {ID = 2, Battery = "100", CurrentUse = "xxxx", Occupancy = "xxxx" },
        new Class1() {ID = 3, Battery = "10", CurrentUse = "xxxx", Occupancy = "xxxx" },
        new Class1() {ID = 4, Battery = "50", CurrentUse = "xxxx", Occupancy = "xxxx" },
        new Class1() {ID = 5, Battery = "80", CurrentUse = "xxxx", Occupancy = "xxxx" },
        new Class1() {ID = 6, Battery = "40", CurrentUse = "xxxx", Occupancy = "xxxx" },
        new Class1() {ID = 7, Battery = "39", CurrentUse = "xxxx", Occupancy = "xxxx" }
    };
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    dgList.ItemsSource = Class1.myList;
}