Imports DevExpress.Xpf.Grid

Public Class tsTableView
    Inherits DevExpress.Xpf.Grid.TableView

    Public Property ColorFuentePersonalizado As Boolean


    Public Sub New()

        ' Esta llamada es exigida por el diseñador.
        MyBase.New

        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub
    Public Sub New(masterRootNode As MasterNodeContainer, masterRootDataItem As MasterRowsContainer, detailDescriptor As DataControlDetailDescriptor)

        ' Esta llamada es exigida por el diseñador.
        MyBase.New(masterRootNode, masterRootDataItem, detailDescriptor)
        InitializeComponent()
        If ColorFuentePersonalizado Then
            Me.RowStyle = Me.FindResource("tsRowStyle")
            Me.CellStyle = Me.FindResource("tsCellStyle")
        End If
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub

    Private Function ShadeBrush(ByVal brush As SolidColorBrush) As SolidColorBrush
        Dim originalColor As Color = brush.Color
        Dim coefficient As Single = 0.9F
        Dim a As Byte = originalColor.A
        If Not Grid.IsKeyboardFocusWithin Then a = CByte((originalColor.A / 2))
        Dim r As Byte = CByte((originalColor.R * coefficient))
        Dim g As Byte = CByte((originalColor.G * coefficient))
        Dim b As Byte = CByte((originalColor.B * coefficient))
        Return New SolidColorBrush(Color.FromArgb(a, r, g, b))
    End Function

    Private Sub TableView_CustomRowAppearance(sender As Object, e As CustomRowAppearanceEventArgs)
        If e.RowSelectionState <> SelectionState.None Then
            Dim result As Object = e.ConditionalValue

            If e.Property Is TextBlock.ForegroundProperty OrElse e.Property Is TextBlock.BackgroundProperty Then
                Dim original As SolidColorBrush = TryCast(e.OriginalValue, SolidColorBrush)
                Dim conditional As SolidColorBrush = TryCast(e.ConditionalValue, SolidColorBrush)
                If Me.ColorFuentePersonalizado AndAlso e.Property Is TextBlock.ForegroundProperty Then
                    Dim row = Me.Grid.GetRow(e.RowHandle)
                    conditional = TryCast(row.GetType.GetProperty("ColorFuenteFila").GetValue(row), SolidColorBrush)
                End If
                If conditional IsNot Nothing AndAlso (original Is Nothing OrElse original.Color <> conditional.Color) Then result = ShadeBrush(conditional)
            End If
            e.Result = result
            e.Handled = True
        End If
    End Sub


    Private Sub tsTableView_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        If ColorFuentePersonalizado Then
            Me.RowStyle = Me.FindResource("tsRowStyle")
            Me.CellStyle = Me.FindResource("tsCellStyle")
        End If
    End Sub
End Class