I am creating columns in code behind. I am also assigning a style selector to each of these columns as below,
Dim EventName As String = "Event1"
Task2Column.DataMemberBinding = New System.Windows.Data.Binding("TASK2") Task2Column.CellStyleSelector = TryCast(Me.Resources("Selector"), StyleSelector)The Style Selector is setup as below in XAML,
<my:ConditionalStyleSelector x:Key="Selector" ColumnToCheck ="TASK1"/>The ConditionalStyleSelector class is as below,
Imports System.Linq Imports System.Text Imports System.Windows Imports System.Windows.Media Imports System.Windows.Data Imports Telerik.Windows.Controls Public Class ConditionalStyleSelector Inherits StyleSelector Public Enum CellColumn TASK1 TASK2 TASK3 TASK4 TASK5 TASK6 TASK7 TASK8 TASK9 TASK10 TASK11 TASK12 TASK13 TASK14 TASK15 TASK16 TASK17 TASK18 TASK19 TASK20 TASK21 TASK22 TASK23 TASK24 TASK25 End Enum Public Enum EventtoCheck Event1 Event2 Event3 End Enum Public Overloads Overrides Function SelectStyle(ByVal item As Object, ByVal container As System.Windows.DependencyObject) As System.Windows.Style Dim style As Style = Nothing
Case EventToCheck.Event1
Dim Event1 As Event1
If TypeOf item Is Event1 Then Event1 = TryCast(item, Event1) Select Case ColumnToCheck Case CellColumn.TASK1 style = PickStyle(Event1.TASK1) Exit Select Case CellColumn.TASK1 style = PickStyle(Event1.TASK2) Exit Select Case CellColumn.TASK3 style = PickStyle(Event1.TASK3) Exit Select Case Else style = PickStyle(0) Exit Select End Select End If
Exit Select
Case EventToCheck.Event2
Dim Event2 As Event2
If TypeOf item Is Event2 Then Event1 = TryCast(item, Event2) Select Case ColumnToCheck Case CellColumn.TASK1 style = PickStyle(Event2.TASK1) Exit Select Case CellColumn.TASK1 style = PickStyle(Event2.TASK2) Exit Select Case CellColumn.TASK3 style = PickStyle(Event2.TASK3) Exit Select Case Else style = PickStyle(0) Exit Select End Select End If
Exit Select
Case EventToCheck.Event3
Dim Event3 As Event 3
If TypeOf item Is Event3 Then Event1 = TryCast(item, Event3) Select Case ColumnToCheck Case CellColumn.TASK1 style = PickStyle(Event3.TASK1) Exit Select Case CellColumn.TASK1 style = PickStyle(Event3.TASK2) Exit Select Case CellColumn.TASK3 style = PickStyle(Event3.TASK3) Exit Select Case Else style = PickStyle(0) Exit Select End Select End If
Exit Select
End Select
Return style End Function Public Property ColumnToCheck() As CellColumn ' WHAT GOES HERE????
End Property Private m_ColumnToCheck As CellColumn Public Property EventToCheck() As EventtoCheck ' WHAT GOES HERE????
End Property Private m_EventToCheck As EventtoCheck Private Function PickStyle(ByVal value As Object) As Style Dim converterDictionary As New ResourceDictionary() With { _ .Source = New Uri("/AcxiomBusinessApplication;component/Assets/Styles.xaml", UriKind.RelativeOrAbsolute) _ } If value Is Nothing Then Return TryCast(converterDictionary("NullCellRedStyle"), Style) ElseIf value = True Then Return TryCast(converterDictionary("CheckedCellGreenStyle"), Style) Else Return Nothing End If End Function End Class As you can see the style class depends on two enumerables "ColumnToCheck" and "EventToCheck". I need to be able to pass these to the ConditionalStyleSelector class when it is called from code behind. But I have no idea how to do that.
I know that in XAML I can call it as such, but even this doesnt work because I need to pass a variable to "EventToCheck which can be one of three values.
<my:ConditionalStyleSelector x:Key="Selector" ColumnToCheck ="TASK1" EventToCheck="Event1 or Event2 or Event 3"/> How can I pass the "ColumnToCheck" and "EventToCheck" from code behind. For example like so,
Dim Task2 As String = EventAdmin.Task2.ToString Dim Task2Column As New GridViewCheckBoxColumn() Task2Column.Width = 50 Task2Column.UniqueName = "Task2Column" Dim border2 As New Grid() border2.Height = 170 border2.Margin = New Thickness(1, 0, -151, 0) Dim textBlock2 As New TextBlock() textBlock2.Text = Task2 textBlock2.VerticalAlignment = VerticalAlignment.Bottom textBlock2.HorizontalAlignment = HorizontalAlignment.Left textBlock2.FontSize = 11 textBlock2.FontWeight = FontWeights.Normal textBlock2.TextAlignment = TextAlignment.Center textBlock2.UseLayoutRounding = True textBlock2.TextTrimming = TextTrimming.None textBlock2.Margin = New Thickness(9, 0, 0, 0) Dim transform2 As New RotateTransform() transform2.Angle = 270 textBlock2.RenderTransform = transform2 border2.Children.Add(textBlock2) Task2Column.Header = border2 Task2Column.IsThreeState = True Dim EventName As String = "Event1" Task2Column.DataMemberBinding = New System.Windows.Data.Binding("TASK2") Task2Column.CellStyleSelector = New ConditionalStyleSelector ' Need to pass EventName and "TASK2" to the "New ConditionalStyleSelector()" Me.CheckListItemssDataGrid.Columns.Add(Task2Column) End If End IfI am a newbie so I really appreciate your help.
I tried this but it threw a bunch of errors when I ran the app
Task2Column.CellStyleSelector = New ConditionalStyleSelector() With { .ColumnToCheck = "TASK2" .EventToCheck = "Event1" }