Skip to main content Skip to footer

Cell Text with Strikethrough in C1Flexgrid for Silverlight

With the Silverlight versions evolving day by day, expectations of application designers to enhance the UI is increasing. It won't be wrong to say that Silverlight has been successful to keep pace with their expectations. However, while I was thinking so, I came across a small limitation in Silverlight. At the time of writing this blog and Silverlight 5.0 already in use, it was surprising to see that Silverlight is yet to provide a Text Strikethrough font. With lot of formatting options available, the only Text Decoration feature available is 'Underline'. You can simply use this code to get an underline text. <TextBlock TextDecorations="underline">Hi there</TextBlock> But what about Strikethrough feature? After visiting lot of web pages, I found that only solution is to customize the UI, by placing a Line object over the TextBlock. In this blog, I am putting up a small code implementation for C1FlexGrid for Silverlight to show a Strikethrough text in the grid cells. To implement this we will use the CellFactory Class to customize the Cell content.

Public Class MyCellFactory  
    Inherits C1.Silverlight.FlexGrid.CellFactory  
    Public Overrides Sub CreateCellContent(grid As C1.Silverlight.FlexGrid.C1FlexGrid, bdr As System.Windows.Controls.Border, rng As C1.Silverlight.FlexGrid.CellRange)  
        MyBase.CreateCellContent(grid, bdr, rng)  

        If rng.Column = 0 Then  
            Dim obj = bdr.Child  
            If obj.GetType() Is GetType(TextBlock) Then  
                If Not CType(obj, TextBlock).Text = String.Empty Then  
                    bdr.Child = Nothing  
                    Dim width As Integer = 0  
                    Dim height As Integer = 0  
                    If CType(obj, TextBlock) IsNot Nothing Then  
                        width = CType(obj, TextBlock).ActualWidth  
                        height = CType(obj, TextBlock).ActualHeight  
                    End If  
                    Dim ln As New Line  
                    ln.Stroke = New SolidColorBrush(Colors.Red)  
                    ln.StrokeThickness = 1  
                    ln.X1 = 0  
                    ln.Y1 = (height / 2) + 3  
                    ln.X2 = width + 1  
                    ln.Y2 = (height / 2) + 3  
                    Dim grd As New Grid  
                    DirectCast(obj, TextBlock).Foreground = New SolidColorBrush(Colors.Green)  
                    bdr.Child = grd  
                End If  
            End If  
        End If  

    End Sub  
End Class  

Applying the above CellFactory object to your C1FlexGrid control will get the text Strikethrough effect. While ending this blog here, I hope we will soon see this feature implemented by default in future Silverlight versions. Download the samples for complete implementation. Download VB Sample Download C# Sample


comments powered by Disqus