0

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

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

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

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

转换器中的断点被命中,但并非针对所有记录(仅 2 或 3 个)。

4

1 回答 1

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;
        }
    }
}
于 2012-09-04T19:51:08.500 回答