2012-06-12 206 views
0

我有xamDataGrid綁定到DataTable其中第一列包含參考值。所有其他列的着色取決於單元格中的值是否等於參考列的值。邏輯使用轉換器。更改xamDataGrid單元格中的顏色

我想要實現的是當我將另一列移動到第一個位置時,它將成爲參考列,所有其他列中的顏色應該改變。

我在聽FieldPositionChanged事件和無效的網格佈局,但它不工作:

grid.UpdateLayout(); 
grid.InvalidateVisual(); 

在轉換器命中該斷點,但不是所有的記錄(只有2或3)。

+0

您能否提供您正在使用的樣式? – alhalama

回答

1

如果在字段移動時設置CellValuePresenterStyle,它們應該正確更新。以下邏輯將執行此操作:

void XamDataGrid1_FieldPositionChanged(object sender, Infragistics.Windows.DataPresenter.Events.FieldPositionChangedEventArgs e) 
{ 
    FieldLayout layout = e.Field.Owner; 
    Field first = null; 
    foreach (Field f in layout.Fields) 
    { 
     if (f.ActualPosition.Column == 0) 
      first = f; 
    } 
    if (first != null) 
    { 
     SetCellValuePresenterStyle(e.Field.Owner, first); 
    } 
} 

void XamDataGrid1_FieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e) 
{ 
    SetCellValuePresenterStyle(e.FieldLayout, e.FieldLayout.Fields[0]); 
} 

void SetCellValuePresenterStyle(FieldLayout layout, Field sourceField) 
{ 
    Binding sourceValueBinding = new Binding("DataItem[" + sourceField.Name + "]"); 
    foreach (Field f in layout.Fields) 
    { 
     if (f != sourceField) 
     { 
      Style cellValuePresenterStyle = new Style(typeof(CellValuePresenter)); 
      Binding compareValueBinding = new Binding("DataItem[" + f.Name + "]"); 
      MultiBinding styleBinding = new MultiBinding(); 
      styleBinding.Bindings.Add(sourceValueBinding); 
      styleBinding.Bindings.Add(compareValueBinding); 
      styleBinding.Converter = new EqualMultiValueConverter(); 
      DataTrigger trigger = new DataTrigger(); 
      trigger.Value = true; 
      trigger.Binding = styleBinding; 
      cellValuePresenterStyle.Triggers.Add(trigger); 
      Setter backgroundSetter = new Setter(Control.BackgroundProperty, Brushes.Green); 
      trigger.Setters.Add(backgroundSetter); 
      f.Settings.CellValuePresenterStyle = cellValuePresenterStyle; 
     } 
     else 
     { 
      f.Settings.CellValuePresenterStyle = null; 
     } 
    } 
} 
+0

謝謝!稍後會嘗試 –