2017-03-07 214 views
0

我製作了一個包含ButtonDataGrid的UI。我已在代碼的一些計算方法,產生四個列表看起來像這樣:根據最小值更改DataGridRow顏色

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; 

,並沒有什麼作品。

謝謝。

+0

你想整行變色?或者只有一個單元格在行中? –

+0

整個行在索引[我]伴侶。 – Ben

+0

dataGridView1是什麼類型的控件?您的示例代碼似乎顯示了您設置用於顯示列表的dataGrid的不同控件。 –

回答

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; 
} 

only problem is when i scroll down (i have many rows), it colors few rows instead of finding only 1 min and then i get an error: "NullReferenceEXception was unhandled"

您可以在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(); 
     } 
    } 
} 
+0

我試過了,我得到了「包含與路由事件相關的狀態信息和事件數據」 – Ben

+0

@ mm8您忘記了第二個參數的變量名稱。 –

+0

Thank you both =] – Ben