With this blog, we will be discussing on how to implement customizations on the Row icons in C1FlexGrid for LightSwitch.

As we already know about the CellFactory class, which is responsible for creating every cell shown on the grid, we will use the same in order to place Custom Images on the default Row icons being shown in case of Selected Row, New Row and Dirty Row.

What are these icons ?

Selected Row Icon - The icon which we see whenever a Row gets selected in the grid
New Row Icon - The icon which is displayed for adding a new Row
Dirty Row Icon - The icon which is displayed for the Row which is currently being edited

Here is how the Default icons look like :
DefaultIcons

Here, we will first identify the currently Selected Row, New Row and the Dirty Row in the Grid. Then, we will remove the default icons and place the new ones. When doing this, we will override the ApplyCellStyles method of the default CellFactory class and will apply different images to these icons.

The code snippet below implements the same :



public class ConditionalCellFactory : C1.LightSwitch.FlexGrid.Presentation.Controls.LSCellFactory
{
public ConditionalCellFactory(C1.Silverlight.FlexGrid.C1FlexGrid grid)
: base(grid)
{

}
public override void ApplyCellStyles(C1.Silverlight.FlexGrid.C1FlexGrid grid, CellType cellType, CellRange rng, System.Windows.Controls.Border bdr)
{
base.ApplyCellStyles(grid, cellType, rng, bdr);

//Check for the RowHeaders
if ((cellType == CellType.RowHeader))
{
//Check for the currently Selected Row
if (rng.Row == grid.SelectedIndex)
{
bdr.Child = null;
bdr.Child = SetImage(1);
}

//Check for the New Row
if (grid.Rows[rng.Row].DataItem.GetType().ToString() == "Microsoft.LightSwitch.Presentation.Framework.NewItemPlaceholderDataContext")
{
bdr.Child = null;
bdr.Child = SetImage(2);
}

//Check for the Dirty Row - Current Row in which the changes are being done
if (grid.SelectedIndex > -1)
{
if (grid.Rows[grid.SelectedIndex].DataItem.ToString() == "")
{
if (rng.Row == grid.SelectedIndex)
{
bdr.Child = null;
bdr.Child = SetImage(3);
}
}
}
}
}

public Image SetImage(int selected_case)
{
Image myImage = new Image();
BitmapImage bi = new BitmapImage();

if (selected_case == 1)
{
bi.UriSource = new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/SelectedRow.jpeg", UriKind.Relative);
myImage.Stretch = Stretch.Fill;
myImage.Source = bi;
return myImage;
}
if (selected_case == 2)
{
bi.UriSource = new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/AddNew.jpeg", UriKind.Relative);
myImage.Stretch = Stretch.Fill;
myImage.Source = bi;
return myImage;
}
if (selected_case == 3)
{
bi.UriSource = new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/DirtyRow.jpg", UriKind.Relative);
myImage.Stretch = Stretch.Fill;
myImage.Source = bi;
return myImage;
}
else
return null;

}

}


Assign the instance of this class to the CellFactory property of the C1FlexGrid object being used in the application :


partial void CustomRowIcons_FlexGrid_Created()
{
IContentItemProxy proxy = this.FindControl("C1FlexGrid");
proxy.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(proxy_ControlAvailable);
}

void proxy_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
_flex = e.Control as C1.Silverlight.FlexGrid.C1FlexGrid;

_flex.CellFactory = new ConditionalCellFactory(_flex);
}


Note that all the images are added in the ‘Desktop.Client’ project with ‘Build’ type of the images set as ‘Resource’.

CustomRowIcons