I had a problem before where I build dynamic system (light dataset) for doing CRUD (Create, Read, Update and Delete) and I need to store the history of previous record before Update/Delete and at the same time I also need to indicate the column(e.g changing the color of the column) that has been changed per record in history table. I’ve been struggling for a day in order to accomplish this.
1. You need to add reference and import System.Windows.Interactivity from SL3 SDK
2. Create a TableHistoryColorConverter.vb
Imports System.Windows.Data
Imports System.Globalization
Imports System.Reflection
Public Class TableHistoryColorConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim cellColor As SolidColorBrush = New SolidColorBrush(Colors.Black)
Try
Dim columnList As String = value.ToString()
Dim currentColumnName As String = parameter.ToString()
'Dim changeColumnParam As String = parameter.ToString()
Dim changedColumnList As String() = columnList.Split(New Char() {","c})
If changedColumnList.Contains(currentColumnName) Then
cellColor = New SolidColorBrush(Colors.Red)
End If
Catch ex As Exception
End Try
Return cellColor
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New Exception("Not Implemented")
End Function
End Class
3. Create a vb class file called HistoryRecordFormatting.vb
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Windows.Interactivity
Imports Telerik.Windows.Controls
Imports Telerik.Windows.Controls.GridView
Imports System.Windows.Data
Imports SLCoreLib
Public Class HistoryRecordFormatting
Inherits Behavior(Of RadGridView)
Protected Overrides Sub OnAttached()
MyBase.OnAttached()
AddHandler AssociatedObject.RowLoaded, New EventHandler(Of Telerik.Windows.Controls.GridView.RowLoadedEventArgs)(AddressOf AssociatedObject_RowLoaded)
End Sub
Private Sub AssociatedObject_RowLoaded(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridView.RowLoadedEventArgs)
If (TypeOf e.Row Is GridViewHeaderRow) OrElse (TypeOf e.Row Is GridViewFooterRow) OrElse (TypeOf e.Row Is GridViewNewRow) Then
Return
End If
For Each cell In e.Row.Cells
'we want to apply logic to data rows only
'ChangedColumns is the list of the column that has been edited (comma separated value) for that history record
Dim colorBinding As New Binding("ChangedColumns") With { _
.Converter = New TableHistoryColorConverter(), .ConverterParameter = cell.Column.UniqueName _
}
cell.SetBinding(GridViewCell.ForegroundProperty, colorBinding)
Next
'e.Row.Cells(1).SetBinding(GridViewCell.ForegroundProperty, colorBinding)
'e.Row.Cells[1].SetBinding(GridViewCell.BackgroundProperty, colorBinding);
End Sub
End Class
4. Add behavior in your XAML (Interaction.Behaviors) tag and HistoryRecordFormatting.
Basically what it does is adding the converter on every single cell and use the column name to compare with the changed column. It uses Column.UniqueName to pass it to the ConverterParameter and check whether it’s on the list of changed column or not. The problem looks simple at the beginning but when it comes to silverlight then it’s different mechanism of applying the format compared with ASP.NET
The behavior and AssociatedObject_RowLoaded for me looks like itemdatabound event on repeater.