Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

WPF: Styling controls at runtime in Visual Basic code

In WPF applications we usually assign styles or control templates to user controls in XAML code. This is the typical situation when you have a predefined number of controls in the user interface. But sometimes there are situations where you will need to instantiate user controls at runtime in Visual Basic code so you could also need to assign styles at runtime.


Let’s make a simple but practical example. Suppose you have a simple style that you declared inside the window’s resources:



        <Style x:Key="myStyle" TargetType="Button">

            <Setter Property="Width" Value="100"/>

            <Setter Property="Height" Value="30"/>

            <Setter Property="Margin" Value="5"/>




Very simple J Now imagine that you want to assign the above style to a button arranged inside a StackPanel. In XAML code you could write the following:


    <StackPanel Name="Stack1">

        <Button Style="{StaticResource myStyle}" Name="FirstButton"/>



Nothing difficult, as you can see. Now let’s switch to the Visual Basic code. Suppose you want to instantiate a new Button when the application is running and assign the above style to the new button.


We can act as follows:


Class Window1

    Private WithEvents AnotherButton As Button


    Public Sub New()


        ' This call is required by the Windows Form Designer.



        'We can use FindResource to retrieve the desired resource that must be converted into the appropriate type

        Me.AnotherButton = New Button With {.Style = CType(FindResource("myStyle"), Style)}

        'We must remember to add the new control to the collection of controls exposed by the panel



    End Sub

End Class


So we have assigned to the Style property of the user control the content of the resource where the style is defined. Such resource has been retrieved via the FindResource method and then converted into the appropriate type (this is needed because FindResource can retrieve as Object all kinds of resources, so a cast is needed).


If you try to run the above shown code, you’ll see a Window (not so beautiful, I know J) with two buttons, one declared in XAML code and one instantiated at runtime but both have been assigned the same Style.


Obviously we can apply this technique to each control exposing the Style property moreover it doesn’t work only with styles but also with control templates. For example we could declare a control template to override the layout in some Buttons and assign the new template at runtime as well as we saw above regarding a Style.


The following figure shows an example of what we could do with a control template (that I defined with Microsoft Expression Blend):




The code for this control template is quite complex but I have included it into a downloadable solution where you can find both the style version and the control template version. You can download the Visual Basic 2008 source code from here.



Print | posted on mercoledì 21 gennaio 2009 19:17 | Filed Under [ Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 8 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET