如何为Datagrid中的第一行设置颜色 [英] How to set color for first row in Datagrid

查看:227
本文介绍了如何为Datagrid中的第一行设置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf(mvvm)项目中有一个datagrid。
datagrid排序显示添加到集合的最后一个项目作为第一行。
我想为第一行着色(为了突出显示添加到集合中的新项目)
我已经看到了一些类似的问题,但没有一个与我正在寻找的东西有关。
i尝试使用IValueConverter,但它似乎并不适合我,因为我需要获得第一行的唯一标识符,并更改所有其余的行,以将其分类为第一行。



我的集合中的项目的对象模型如下所示:

 code> public class消息
{
public string Date {get; set;}
public string Sender {get; set;}
public string Content {get;设置;}
}

*编辑
忘记添加转换器代码。 ..
当然这会把所有的行颜色变成红色,因为我不知道当收藏变化时如何影响其他行。

  class DateToColorConverter:IValueConverter 
{
对象IValueConverter.Convert(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
if( Convert.ToDateTime(value)> = DateTime.Now.AddMinutes(-1))
{
returnRed;
}
else
返回黄色;
}

对象IValueConverter.ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
throw new NotImplementedException();
}
}


解决方案

你可以使用 RelativeSource 模式 设置为 PreviousData 来标识dataGrid行是否是第一行。对于第一行,PreviousData将返回null。



在ItemContainerStyle中的DataGridRow上应用DataTrigger:

 < DataGrid> 
< DataGrid.ItemContainerStyle>
< Style TargetType =DataGridRow>
< Setter Property =BackgroundValue =LightBlue/>
< Style.Triggers>
< DataTrigger
Binding ={Binding RelativeSource = {RelativeSource Mode = PreviousData}}
Value ={x:Null}>
< Setter Property =BackgroundValue =Green/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGrid.ItemContainerStyle>
< / DataGrid>


I have a datagrid on a wpf (mvvm) project. the datagrid sort is showing the last item added to the collection as the first row. i want to color the first row (in order to highlight new item added to the collection) I've seen some similar questions about this manner but none of them are really related to what i am looking for. i have tried to use a IValueConverter but it doesnt seems to be the right path for me as i need to get a unique identifier for the first row and change all the rest of the rows in order to classified it as a "First Row".

my object model for the items in the collection looks like this:

public class Messages
{
    public string Date {get; set;}
    public string Sender{get; set;}
    public string Content{get; set;}
} 

*EDIT Forgot to add the converter code... of course this will color all rows to red, as i dont know how to affect the other rows when the collection changes.

class DateToColorConverter : IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (Convert.ToDateTime(value) >= DateTime.Now.AddMinutes(-1))
        {
            return "Red";
        }
        else
            return "Yellow";
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

解决方案

You can use RelativeSource with Mode set to PreviousData to identify whether dataGrid row is first one or not. For first row PreviousData will return null.

Apply DataTrigger on DataGridRow in ItemContainerStyle:

<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>
</DataGrid>

这篇关于如何为Datagrid中的第一行设置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆