Question

I have xamDataGrid bound to DataTable where first column contains reference values. Coloring of all other columns depends on whether the value in cells is or isn't equal to the value of reference column. The logic uses converter.

What I want to achieve is when I move another column to the 1st position, it will become the reference column and the colors in all other columns should change.

I'm listening to FieldPositionChanged event and invalidating the grid layout, but it does not work:

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

The breakpoint in converter is hit but not for all records (only 2 or 3).

Was it helpful?

Solution

If you set the CellValuePresenterStyle when the fields move they should update correctly. The following logic will do this:

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;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top