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

Generic way to pass a telerik ThemePalette to a sub/function?

3 Answers 10 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
Rob A. asked on 29 Oct 2020, 11:57 PM

I'm trying to pass  a Telerik CrystalPalette to a Sub but I get an error "...is class type and can not be used as an expression".  Hints?

Public Function DoSomething() As Boolean
 
   ApplyThemeFontSizes(CrystalTheme)
 
End Function
 
Private Sub ApplyThemeFontSizes(Of T)()
 
        Try
 
            T.Palette.FontSizeXS = 8 + Me.FontOffset
            T.Palette.FontSizeS = 10 + Me.FontOffset
            T.Palette.FontSize = 12 + Me.FontOffset
            T.Palette.FontSizeL = 14 + Me.FontOffset
            T.Palette.FontSizeXL = 16 + Me.FontOffset
 
        Catch ex As Exception
 
            ' TODO: Log error to file (possible the Theme doesn't have a "Palette")
 
        End Try
 
    End Sub

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 30 Oct 2020, 03:37 PM

Hello Robin,

The Palette property of each palette is defined in each separate palette class. It is not inherited, so you won't be able to use a generic method without parameters. Each palette is unique and has a set of properties which may vary in naming. This is most valid for the brushes of the palette. The FontSize properties are also not inherited from the child ThemePalette class. In other words, you will need to check for the specific palette implementation and set its FontSize.

On way to achieve your requirement without hard-coding the palettes or if-else statements that check them is to use a reflection. For example:

Public Sub DoSomething()
	ApplyThemeFontSizes(CrystalPalette.Palette)
End Sub

Public Property FontOffset As Double = 10

Private Sub ApplyThemeFontSizes(ByVal palette As ThemePalette)
	TrySetPaletteProperty(palette, "FontSizeXS", 8 + Me.FontOffset)
	TrySetPaletteProperty(palette, "FontSizeS", 10 + Me.FontOffset)
	TrySetPaletteProperty(palette, "FontSize", 12 + Me.FontOffset)
	TrySetPaletteProperty(palette, "FontSizeL", 14 + Me.FontOffset)
	TrySetPaletteProperty(palette, "FontSizeXL", 16 + Me.FontOffset)
End Sub

Private Function TrySetPaletteProperty(ByVal palette As ThemePalette, ByVal propertyName As String, ByVal newValue As Object) As Boolean
	Dim propertyInfo = palette.[GetType]().GetProperty(propertyName, System.Reflection.BindingFlags.[Static] Or System.Reflection.BindingFlags.[Public])
	If propertyInfo IsNot Nothing Then
		propertyInfo.SetValue(palette, newValue)
		Return True
	End If
	Return False
End Function

I hope this helps. 

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 03 Nov 2020, 07:25 AM

Martin,

Thanks for the response, unfortunately your code sample doesn't work ... propertyInfo always comes back as Nothing so the SetValue never happens.

NOTE: verified the palette does exist for the selected theme

Cheers, Rob.

0
Martin Ivanov
Telerik team
answered on 03 Nov 2020, 11:30 AM

Hello Robin,

It looks like the GetProperty() method doesn't need the BindingFlags in VB.NET. To get the property info, remove the flags from the GetProperty() method call. 

Dim propertyInfo = palette.GetType().GetProperty(propertyName)

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Martin Ivanov
Telerik team
Rob A.
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or