根据最小值更改DataGridRow颜色

时间:2017-03-07 19:39:36

标签: c# wpf list datagrid background-color

我创建了一个包含ButtonDataGrid的用户界面。我在代码中做了一些计算,生成了四个列表,如下所示:

var L = new List<MyDataObject>();
for (int z = 0; z < list_Exp.Count; z++)
{
    var d = new MyDataObject();

    d.AmountNeed = Math.Ceiling((goalexp - currentexp) / (list_Exp[z]));
    d.TotalLose = d.AmountNeed * (list_Amount_MadeFrom_One[z] * list_BuyPrice_MadeFrom_One[z] + list_Amount_MadeFrom_Two[z] * list_BuyPrice_MadeFrom_Two[z]);
    d.TotalGain = d.AmountNeed * list_AmountMade[z] * list_SellPrice[z];
    d.TotalCost = d.TotalGain - d.TotalLose;

    L.Add(d);
}

获得列表后,我会在特定列表中找到最小值:

int i = L.FindIndex(x => x.TotalCost == L.Min(y => y.TotalCost));

并将所有列表添加到dataGrid

dataGrid.ItemsSource = L;

现在,我一直在尝试将Rows[i]的颜色更改为绿色或任何其他颜色。我尝试了类似的东西:

grid.Columns["NameOfColumn"].DefaultCellStyle.ForeColor = Color.Gray;

dataGrid.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;

没有任何作用。

谢谢。

1 个答案:

答案 0 :(得分:3)

例如,您可以定义RowStyle并处理Loaded容器的DataGridRow事件,如下所示:

<DataGrid x:Name="dataGrid">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="Loaded" Handler="RowLoaded" />
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
private void RowLoaded(object sender, RoutedEventArgs e)
{
    DataGridRow dgr = sender as DataGridRow;
    MyDataObject x = dgr.DataContext as MyDataObject;
    if (x.TotalCost == dataGrid.Items.OfType<MyDataObject>().Min(y => y.TotalCost))
        dgr.Background = Brushes.Green;
}
  

唯一的问题是当我向下滚动(我有很多行)时,它会变色几行而不是只找到1分钟然后我得到一个错误:“NullReferenceEXception未处理”

您可以将Background容器的DataGridRow属性绑定到当前MyDataObject和对象集合,然后使用MultiValueConverter

<DataGrid x:Name="dataGrid" xmlns:local="clr-namespace:WpfApplication1">
    <DataGrid.Resources>
        <local:Converter x:Key="conv" />
    </DataGrid.Resources>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource conv}">
                        <Binding Path="." />
                        <Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
namespace WpfApplication1
{
    public class Converter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            MyDataObject x = values[0] as MyDataObject;
            if(x != null)
            {
                IEnumerable<MyDataObject> collection = values[1] as IEnumerable<MyDataObject>;
                if(collection != null && x.TotalCost == collection.Min(y => y.TotalCost))
                    return System.Windows.Media.Brushes.Green;
            }

            return System.Windows.DependencyProperty.UnsetValue;
        }

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