更改 xamDataGrid 单元格中的颜色



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

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

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

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

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

如果在字段移动时设置 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;
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新