1. 程式人生 > >Lightswitch C1FLexGrid 自定義列顯示格式的幾種寫法

Lightswitch C1FLexGrid 自定義列顯示格式的幾種寫法

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;
            }
        }