C#-改变表格行颜色

,

简单的改变需求

通过setter设置颜色值,可直接改变,不需要条件判断的简单需求

<DataGrid ItemsSource="{Binding TableColorModels}">
    <DataGrid.RowStyle >
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="LightPink"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="LightGray"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="#90F670"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Header="第一项" Binding="{Binding item1}"></DataGridTextColumn>
        <DataGridTextColumn Header="第二项" Binding="{Binding item2}"></DataGridTextColumn>
        <DataGridTextColumn Header="第三项" Binding="{Binding item3}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

需要通过表格值进行改变时

我们可以通过转换器IValueConverter 或者IMultiValueConverter获取到数据,然后根据你想要的条件进行颜色值的返回。

//TableColorView.xaml
<Window x:Class="Demo.TableColorView"
        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:Demo"
        xmlns:common="clr-namespace:Demo.Common"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <common:MultiBindingConverter x:Key="MultiBindingConverter" />
    </Window.Resources>
    <Grid>
            <DataGrid ItemsSource="{Binding TableColorModels}">
                <DataGrid.RowStyle >
                    <Style TargetType="DataGridRow">
                        <Setter Property="Background">
                            <Setter.Value>
                                <MultiBinding Converter="{StaticResource MultiBindingConverter}">
                                    <Binding Path="."/>
                                </MultiBinding>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="第一项" Binding="{Binding item1}"></DataGridTextColumn>
                    <DataGridTextColumn Header="第二项" Binding="{Binding item2}"></DataGridTextColumn>
                    <DataGridTextColumn Header="第三项" Binding="{Binding item3}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
    </Grid>
</Window>
//MultiBindingConverter.cs
using System;
using System.Windows.Data;
using System.Windows.Media;

namespace Demo.Common
{
    /// <summary>
    /// 绑定多参数的值转换器
    /// </summary>
    public class MultiBindingConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null)
            {
                return Brushes.LightBlue;
            }
            else
            {
                return "";
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

效果预览

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

微信公众号

微信小程序