1.最简单的方式,在控件列属性里设置,注(列的控件类型必需设为FLEX自带的那种才行), 有FORMAT CONDITION 可以设置,包括色彩等。一列只能定义一种规则。
2. 实现CellFactory ,自定义规则
public class ConditionalCellFactory : CellFactory
{
public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange rng, System.Windows.Controls.Border bdr)
{
base.ApplyCellStyles(grid, cellType, rng, bdr);
if ((cellType == CellType.Cell) && (rng.Column == 13)) // 对第几列处理
{
var row = grid.Rows[rng.Row];
var cst = row.DataItem as OrderDetailExecuteDataItem; //获取当前行的实际对象值转一下
if (cst != null)
{
if (cst.DeliveryDate.Date > DateTime.Today)
bdr.Background = new SolidColorBrush(Colors.Blue);
else
bdr.Background = new SolidColorBrush(Colors.Red);
}
}
}
}
partial void FlexibleOrderDetailExecuteDataBySupplierIDGrid_Created()
{
IContentItemProxy proxy = this.FindControl("C1FlexGrid");
proxy.ControlAvailable += (s, e) =>
{
var fg = e.Control as C1FlexGrid;
if (fg != null)
{
fg.CellFactory = new ConditionalCellFactory();
}
};
}
3. 直接调用现有条件格式类(和第一个本质是一样,只是后台用代码实现,但可以定义多个规则)
void proxy_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
C1.Silverlight.FlexGrid.C1FlexGrid _flex =
e.Control as C1.Silverlight.FlexGrid.C1FlexGrid;
LSColumn col = _flex.Columns["Country"] asLSColumn;
TextConditionalFormat f1 = newTextConditionalFormat(Colors.Red, Colors.White, false);
f1.Operator = ConditionOperator.Equals;
f1.Value = "Canada";
TextConditionalFormat f2 = newTextConditionalFormat(Colors.Blue, Colors.White, false);
f2.Operator = ConditionOperator.Equals;
f2.Value = "USA";
TextConditionalFormat f3 = newTextConditionalFormat(Colors.Green, Colors.White, false);
f3.Operator = ConditionOperator.Equals;
f3.Value = "Mexico";
col.ConditionalFormats.Add(f1);
col.ConditionalFormats.Add(f2);
col.ConditionalFormats.Add(f3);
}
或者自己实现一个条件类:
public class CustomFormat : C1.LightSwitch.FlexGrid.Presentation.Controls.NumericConditionalFormat
{
public CustomFormat(string backColor) : base(backColor, null, false)
{
}
public override bool IsValid
{
get { return true; }
}
public override bool Evaluate(object obj)
{
if (IsNumeric(obj))
{
decimal n = System.Convert.ToDecimal(obj);
return (n % 5M) == 0;
}
return false;
}
}