如何在DataGrid的最后一行设置背景颜色?

时间:2015-02-03 18:03:56

标签: wpf datagrid

我正在构建一个自定义控件,并希望添加一个功能,可以设置DataGrid上最后一行的背景颜色。

我的第一个问题是如何选择DataGrid上的最后一行。

第二个问题是如何将背景颜色设置为整行。

1 个答案:

答案 0 :(得分:0)

请参阅以下代码,了解可以设置LastRowColor的自定义控件。我使用了LoadingRow事件来设置Last row color。

class DatagridEx:DataGrid
{
    private static DependencyProperty LastRowColourProperty
    = DependencyProperty.Register("LastRowColour", typeof(Brush), typeof(DatagridEx), new PropertyMetadata());        

    public Brush LastRowColour
    {
        get { return GetValue(LastRowColourProperty) as Brush; }
        set { SetValue(LastRowColourProperty, value); }
    } 

    public DatagridEx()
    {
        this.LoadingRow += DatagridEx_LoadingRow;
    }

    void DatagridEx_LoadingRow(object sender, DataGridRowEventArgs e)
    {            
        int index=this.Items.IndexOf(e.Row.DataContext);
        if (index == this.Items.Count - 1)
        {
            e.Row.Background = LastRowColour;
        }
    }
}
<local:DatagridEx x:Name="dgr" LastRowColour="Red">

    </local:DatagridEx>
相关问题