This is a migrated thread and some comments may be shown as answers.

[Solved] Conditional Style in Code Behind

6 Answers 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Timothy
Top achievements
Rank 1
Timothy asked on 27 Nov 2010, 09:36 PM

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 If

I 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"
}


6 Answers, 1 is accepted

Sort by
0
Timothy
Top achievements
Rank 1
answered on 28 Nov 2010, 01:30 AM
Can anyone help me please.
0
Timothy
Top achievements
Rank 1
answered on 29 Nov 2010, 09:53 AM
I really need help with this. Can someone help me?
0
Milan
Telerik team
answered on 29 Nov 2010, 10:06 AM
Hello Timothy,

You can simply create new instances of your Selector class and pass the required parameters. For example, you can create a property on ConditionalStyleSelector called ColumnToCheck and set this property to its respective value:

Dim selector As New ConditionalStyleSelector()
selector.ColumnToCheck = "TASK1"
Task2Column.CellStyleSelector = selector


Regards,
Milan
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Timothy
Top achievements
Rank 1
answered on 29 Nov 2010, 11:31 AM
I implemented it as you suggested and I am recieving a conversion error. I attached the error message.

My style selector class is as follows,

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 Eventitem
        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
        Select Case EventToCheck
            Case Eventitem.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 CellColumn.TASK4
                            Style = PickStyle(Event1.TASK4)
                            Exit Select
                        Case CellColumn.TASK5
                            Style = PickStyle(Event1.TASK5)
                            Exit Select
                        Case CellColumn.TASK6
                            Style = PickStyle(Event1.TASK6)
                            Exit Select
                        Case CellColumn.TASK7
                            Style = PickStyle(Event1.TASK7)
                            Exit Select
                        Case CellColumn.TASK8
                            Style = PickStyle(Event1.TASK8)
                            Exit Select
                        Case CellColumn.TASK9
                            Style = PickStyle(Event1.TASK9)
                            Exit Select
                        Case CellColumn.TASK10
                            Style = PickStyle(Event1.TASK10)
                            Exit Select
                        Case CellColumn.TASK11
                            Style = PickStyle(Event1.TASK11)
                            Exit Select
                        Case CellColumn.TASK12
                            Style = PickStyle(Event1.TASK12)
                            Exit Select
                        Case CellColumn.TASK13
                            Style = PickStyle(Event1.TASK13)
                            Exit Select
                        Case CellColumn.TASK14
                            Style = PickStyle(Event1.TASK14)
                            Exit Select
                        Case CellColumn.TASK15
                            Style = PickStyle(Event1.TASK15)
                            Exit Select
                        Case CellColumn.TASK16
                            Style = PickStyle(Event1.TASK16)
                            Exit Select
                        Case CellColumn.TASK17
                            Style = PickStyle(Event1.TASK17)
                            Exit Select
                        Case CellColumn.TASK18
                            Style = PickStyle(Event1.TASK18)
                            Exit Select
                        Case CellColumn.TASK19
                            Style = PickStyle(Event1.TASK19)
                            Exit Select
                        Case CellColumn.TASK20
                            Style = PickStyle(Event1.TASK20)
                            Exit Select
                        Case CellColumn.TASK21
                            Style = PickStyle(Event1.TASK21)
                            Exit Select
                        Case CellColumn.TASK22
                            Style = PickStyle(Event1.TASK22)
                            Exit Select
                        Case CellColumn.TASK23
                            Style = PickStyle(Event1.TASK23)
                            Exit Select
                        Case CellColumn.TASK24
                            Style = PickStyle(Event1.TASK24)
                            Exit Select
                        Case CellColumn.TASK25
                            Style = PickStyle(Event1.TASK25)
                            Exit Select
                    End Select
                End If
  
                Exit Select
  
            Case Eventitem.Event2
                Dim Event2 As Event2
  
                If TypeOf item Is Event2 Then
                    Event2 = 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 CellColumn.TASK4
                            style = PickStyle(Event2.TASK4)
                            Exit Select
                        Case CellColumn.TASK5
                            style = PickStyle(Event2.TASK5)
                            Exit Select
                        Case CellColumn.TASK6
                            style = PickStyle(Event2.TASK6)
                            Exit Select
                        Case CellColumn.TASK7
                            style = PickStyle(Event2.TASK7)
                            Exit Select
                        Case CellColumn.TASK8
                            style = PickStyle(Event2.TASK8)
                            Exit Select
                        Case CellColumn.TASK9
                            style = PickStyle(Event2.TASK9)
                            Exit Select
                        Case CellColumn.TASK10
                            style = PickStyle(Event2.TASK10)
                            Exit Select
                        Case CellColumn.TASK11
                            style = PickStyle(Event2.TASK11)
                            Exit Select
                        Case CellColumn.TASK12
                            style = PickStyle(Event2.TASK12)
                            Exit Select
                        Case CellColumn.TASK13
                            style = PickStyle(Event2.TASK13)
                            Exit Select
                        Case CellColumn.TASK14
                            style = PickStyle(Event2.TASK14)
                            Exit Select
                        Case CellColumn.TASK15
                            style = PickStyle(Event2.TASK15)
                            Exit Select
                        Case CellColumn.TASK16
                            style = PickStyle(Event2.TASK16)
                            Exit Select
                        Case CellColumn.TASK17
                            style = PickStyle(Event2.TASK17)
                            Exit Select
                        Case CellColumn.TASK18
                            style = PickStyle(Event2.TASK18)
                            Exit Select
                        Case CellColumn.TASK19
                            style = PickStyle(Event2.TASK19)
                            Exit Select
                        Case CellColumn.TASK20
                            style = PickStyle(Event2.TASK20)
                            Exit Select
                        Case CellColumn.TASK21
                            style = PickStyle(Event2.TASK21)
                            Exit Select
                        Case CellColumn.TASK22
                            style = PickStyle(Event2.TASK22)
                            Exit Select
                        Case CellColumn.TASK23
                            style = PickStyle(Event2.TASK23)
                            Exit Select
                        Case CellColumn.TASK24
                            style = PickStyle(Event2.TASK24)
                            Exit Select
                        Case CellColumn.TASK25
                            style = PickStyle(Event2.TASK25)
                            Exit Select
                    End Select
                End If
  
                Exit Select
  
            Case Eventitem.Event3
                Dim Event3 As Event3
  
                If TypeOf item Is Event3 Then
                    Event3 = 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 CellColumn.TASK4
                            style = PickStyle(Event3.TASK4)
                            Exit Select
                        Case CellColumn.TASK5
                            style = PickStyle(Event3.TASK5)
                            Exit Select
                        Case CellColumn.TASK6
                            style = PickStyle(Event3.TASK6)
                            Exit Select
                        Case CellColumn.TASK7
                            style = PickStyle(Event3.TASK7)
                            Exit Select
                        Case CellColumn.TASK8
                            style = PickStyle(Event3.TASK8)
                            Exit Select
                        Case CellColumn.TASK9
                            style = PickStyle(Event3.TASK9)
                            Exit Select
                        Case CellColumn.TASK10
                            style = PickStyle(Event3.TASK10)
                            Exit Select
                        Case CellColumn.TASK11
                            style = PickStyle(Event3.TASK11)
                            Exit Select
                        Case CellColumn.TASK12
                            style = PickStyle(Event3.TASK12)
                            Exit Select
                        Case CellColumn.TASK13
                            style = PickStyle(Event3.TASK13)
                            Exit Select
                        Case CellColumn.TASK14
                            style = PickStyle(Event3.TASK14)
                            Exit Select
                        Case CellColumn.TASK15
                            style = PickStyle(Event3.TASK15)
                            Exit Select
                        Case CellColumn.TASK16
                            style = PickStyle(Event3.TASK16)
                            Exit Select
                        Case CellColumn.TASK17
                            style = PickStyle(Event3.TASK17)
                            Exit Select
                        Case CellColumn.TASK18
                            style = PickStyle(Event3.TASK18)
                            Exit Select
                        Case CellColumn.TASK19
                            style = PickStyle(Event3.TASK19)
                            Exit Select
                        Case CellColumn.TASK20
                            style = PickStyle(Event3.TASK20)
                            Exit Select
                        Case CellColumn.TASK21
                            style = PickStyle(Event3.TASK21)
                            Exit Select
                        Case CellColumn.TASK22
                            style = PickStyle(Event3.TASK22)
                            Exit Select
                        Case CellColumn.TASK23
                            style = PickStyle(Event3.TASK23)
                            Exit Select
                        Case CellColumn.TASK24
                            style = PickStyle(Event3.TASK24)
                            Exit Select
                        Case CellColumn.TASK25
                            style = PickStyle(Event3.TASK25)
                            Exit Select
                    End Select
                End If
  
                Exit Select
  
        End Select
        Return style
    End Function
  
    Public Property ColumnToCheck() As CellColumn
        Get
            Return m_ColumnToCheck
        End Get
        Set(ByVal value As CellColumn)
            m_ColumnToCheck = value
        End Set
    End Property
  
    Private m_ColumnToCheck As CellColumn
  
    Public Property EventToCheck() As Eventitem
        Get
            Return m_EventToCheck
        End Get
        Set(ByVal value As Eventitem)
            m_EventToCheck = value
        End Set
    End Property
  
    Private m_EventToCheck As Eventitem
  
    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
0
Timothy
Top achievements
Rank 1
answered on 29 Nov 2010, 04:41 PM
Can you please help me with thie error?
0
Milan
Telerik team
answered on 30 Nov 2010, 04:04 PM
Hello Timothy,

Is seems that somewhere in this code an invalid cast is made. If you enable "break on unhanded exceptions" in visual Studio you will be able to pinpoint the problematic code. 


Kind regards,
Milan
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
GridView
Asked by
Timothy
Top achievements
Rank 1
Answers by
Timothy
Top achievements
Rank 1
Milan
Telerik team
Share this question
or