更改xamDataGrid单元格中的颜色

时间:2012-06-12 07:56:43

标签: colors drag-and-drop infragistics xamdatagrid

xamDataGrid绑定到DataTable,其中第一列包含参考值。所有其他列的着色取决于单元格中的值是否等于参考列的值。逻辑使用转换器。

我想要实现的是当我将另一列移动到第一个位置时,它将成为参考列,并且所有其他列中的颜色都应该更改。

我正在监听FieldPositionChanged事件并使网格布局无效,但它不起作用:

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

转换器中的断点被命中,但不是所有记录(仅2或3)。

1 个答案:

答案 0 :(得分: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;
        }
    }
}