【问题标题】:Change DataGridRow color according to min value根据最小值更改 DataGridRow 颜色
【发布时间】:2017-07-28 04:03:18
【问题描述】:

我制作了一个包含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;

没有任何效果。

谢谢。

【问题讨论】:

  • 您希望整行都改变颜色?还是只有一行中的一个单元格?
  • 索引 [i] mate 处的整行。
  • dataGridView1是什么类型的控件?您的示例代码似乎显示了与您设置为显示列表的 dataGrid 不同的控件。
  • 我编辑了它,伙计,我的错。我尝试了来自论坛的不同代码。我的控制是dataGridbutton。整个代码都写在按钮类里面。

标签: c# wpf list datagrid background-color


【解决方案1】:

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

<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 未处理”

您可以将DataGridRow 容器的Background 属性绑定到当前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();
        }
    }
}

【讨论】:

  • 我试过了,我得到“包含与路由事件相关的状态信息和事件数据”
  • @mm8 第二个参数的变量名忘记了。
  • 谢谢你们=]
  • @mm8 唯一的问题是当我向下滚动时(我有很多行),它会为几行着色而不是只找到 1 分钟,然后我收到一个错误:“NullReferenceEXception 未处理”
  • 抱歉,它应该是 xmlns:local="clr-namespace:WpfApplication1",前提是您根据我的示例定义了 Converter 类位于“WpfApplication1”命名空间中。我编辑了我的答案。
猜你喜欢
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 2015-09-02
相关资源
最近更新 更多