2013-03-15 50 views
3

我有一個DataGridView,我試圖動態更新取決於一列,兩個人之間的比較結果的行的背景顏色。我的datagridview綁定到一個數據表。 datagridview中的三個不同列是min,max和present。最小和最大列中的值是靜態的,不會更改。每列當前列中的值動態更新。更新datagridview的行背景顏色與datatriggers和的IValueConverter

我使用了一個名爲MinMaxTester類實現IValueConverter接口比較單元格的內容返回一個刷色。

隨着我已經實現瞭解決方案,我注意到背景顏色有時會更新。 datagridview是選項卡控件中的選項卡項目的一部分。當datagridview對用戶不可見時,通常會更新背景顏色。當datagridview對用戶可見時(IE選項卡中的選項卡項目已被選中),背景顏色不會更新。

我想知道什麼,我需要在我的解決方案來改變,使該行的背景顏色會隨時更新?

XAML文件中的代碼

<Window.Resources> 
    <local:MinMaxTester x:Key="MinMaxTester"/> 
</Window.Resources> 

<DataGrid.Columns> 
<DataGridTextColumn Header="Present" Binding="{Binding Present}"/> 
<DataGridTextColumn Header="Min" Binding="{Binding Min}"/> 
<DataGridTextColumn Header="Max" Binding="{Binding Max}"/> 
</DataGrid.Columns> 

<DataGrid.RowStyle> 
    <Style TargetType="{x:Type DataGridRow}"> 
     <Setter Property="Background" Value="{Binding Converter={StaticResource MinMaxTester}}"/> 
    </Style> 
<DataGrid.RowStyle> 

實現代碼

[ValueConversion(typeof(DataRowView),typeof(Brush))] 
public class MinMaxTester: IValueConverter 
{ 
    public object Convert(object datagridrow, Type target, object parameter, System.Globalization.CultureInfo culture) 
    { 
     int min, max, present; 
     DataRowView r = datagridrow as DataRowView; 
     min = int.Parse(r["Min"].ToString()); 
     max = int.Parse(r["Max"].ToString()); 
     present = int.Parse(r["Present"].ToString()); 
     if (present >= min && present <= max) 
      return Brushes.Green; 
     else 
      return Brushes.Red; 
    } 
    public object ConvertBack(object datagridrow, Type target, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException("Not using the ConvertBack function in MinMaxTester"); 

    } 
} 

回答

0

你說你被綁定到DataTable,但是在DataTable數據不實現INotifyPropertyChanged,所以不會養PropertyChange通知告訴UI它需要更新。

我建議切換到您的結合從DataGrid一個DataTableObservableCollection<MyDataObject>,並確保MyDataObject實現INotifyPropertyChanged

1

我不知道你的Present在做什麼,在哪裏,但是這可能工作,你需要什麼( W/O改變你的代碼太多)

<MultiBinding Converter="{StaticResource converter}" Mode="OneWay"> 
    <MultiBinding.Bindings> 
     <Binding Path="Present" /> 
     <Binding Path="" /> 
    </MultiBinding.Bindings> 
</MultiBinding> 

...,然後在轉換器(也就是現在的IMultiValueConverter - 所有類似只是多了一個字段),你有兩個值。你使用'行'來計算。
...你使用直接結合Present觸發的變化。

您還需要確保,無論「持有」今 - 前面已經提到了INotifyPropertyChanged的。

希望它有幫助

+0

謝謝你,你只回答了一個非常類似的問題我這個答案有也。 TIL關於多值轉換器 – 2013-03-16 14:26:53

+0

不客氣@AlexMarshall – NSGaga 2013-03-16 14:43:51

1

你試過嗎?

<Window.Resources> 
     <my:RowBackgroundColorConverter x:Key="rowBackgroundColorConverterResource"></my:RowBackgroundColorConverter> 
    </Window.Resources> 

<DataGrid.RowStyle> 
          <Style TargetType="{x:Type DataGridRow}"> 
           <Setter Property="Background" Value="{Binding fieldXXX, Converter={StaticResource converterXXX}}"></Setter> 
          </Style> 
         </DataGrid.RowStyle> 

和轉換器代碼:

using System; 
using System.Globalization; 
using System.Windows.Data; 
using System.Windows.Media; 
using System.Windows; 
using System.Windows.Controls; 

namespace XXX.Converters 
{ 
    public class RowBackgroundColorConverter : IValueConverter 
    { 
     private readonly Color expiredColor = Colors.Red; 

     private readonly Color normalColor = Colors.Gray; 

     public object Convert(
      object value, 
      Type targetType, 
      object parameter, 
      CultureInfo culture) 
     { 

      if (XXXXX) 
       return new SolidColorBrush(expiredColor); 

       return new SolidColorBrush(normalColor); 
     } 

     public object ConvertBack(
      object value, 
      Type targetType, 
      object parameter, CultureInfo culture) 
     { 
      throw new NotSupportedException(); 
     } 
    } 
}