How To: Draw Dotted Cell Borders in C1FlexGrid

Customization has always been the strong USP for C1FlexGridcontrol whether in Winforms, WPF or Silverlight. Specifically its ability to integrate with the default .Net Classes has made it more popular. Leveraging upon this flexibility, I look to share a small utility implementation to show Dotted Cell Borders in C1FlexGrid for WPF. Concept of Dotted Cell Borders uses VisualBrush. It has a property Visual which allows to define new content inside the border. You can read more on this from here.aspx). We can define a Rectagle as border content which can be customized to show dotted lines using the property 'StrokeDashArray' that indicates the pattern of dashes and gaps that is used to outline shapes. Above concept can be easily applied on individual cells using CellFactory. Refer to the code snippet below for the implementation.


public class MyCellFactory : C1.WPF.FlexGrid.CellFactory  
{  
  List<C1.WPF.FlexGrid.CellRange> crw = new List<C1.WPF.FlexGrid.CellRange>();  
  public MyCellFactory()  
  {  
     crw.Add(new C1.WPF.FlexGrid.CellRange(0,0));  
     crw.Add(new C1.WPF.FlexGrid.CellRange(1,1));  
     crw.Add(new C1.WPF.FlexGrid.CellRange(2,2));  
     crw.Add(new C1.WPF.FlexGrid.CellRange(3,3));          
  }  

  public override Border CreateCellBorder(C1.WPF.FlexGrid.C1FlexGrid grid, C1.WPF.FlexGrid.CellType cellType, C1.WPF.FlexGrid.CellRange rng)  
  {  
     Border bdr = base.CreateCellBorder(grid, cellType, rng);  

     if (cellType == C1.WPF.FlexGrid.CellType.Cell)  
       if (crw.Contains(rng))  
       {  
           VisualBrush vb = new VisualBrush();  
           vb.Visual = new Rectangle() { Height = 10, Width = 40, Stroke = new SolidColorBrush(Colors.Red), StrokeDashArray = new DoubleCollection() { 1.0 } };  

           bdr.BorderThickness = new Thickness(2, 2, 2, 2);  
           bdr.BorderBrush = vb;                  
       }  

    return bdr;  
  }  

}  

Once we assign the above CellFactory object to C1FlexGrid, it would show the dotted cells as shown in the image below. dottedborder Attached sample shows the complete implementation. Download Sample

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus