【问题标题】:Changing background of DataGridTextColumn if value is between a and b如果值在 a 和 b 之间,则更改 DataGridTextColumn 的背景
【发布时间】:2018-05-02 19:28:58
【问题描述】:

所以我试图根据其值更改 Datagrid 中单元格的背景。因此,如果我想根据静态值更改它,我会使用 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>

感谢任何帮助!

【问题讨论】:

标签: c# wpf xaml datagrid


【解决方案1】:

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

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

<Window.Resources>
    <local:myConverter x:Key="myConverter"/>
</Window.Resources>

我的设计;

    <Grid>
        <DataGrid Name="dgList" Background="LightGray" 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}">
                            <Setter Property="Background" Value="{Binding Battery, Converter={StaticResource myConverter}}"></Setter>
                        </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>
    </Grid>

此绑定将您的数据电池(10、30、40、80 等)发送到转换器。转换器返回一个对象,我们的对象必须是 SolidColorBrush 才能将其绑定到 BackGround。仅此​​而已.我就这样解决了

<Setter Property="Background" Value="{Binding Battery, Converter={StaticResource myConverter}}"></Setter>

myConverter 用于将数据转换为背景;

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;
}

我的项目截图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 2022-10-01
    • 2017-12-24
    • 2016-05-04
    • 2018-11-16
    • 2021-12-21
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多