Always Set WPF Templates using Styles or Resources

This is based on my WPF experience regarding templates. I know it’s not too detailed, but believe me: it’s better for you if you always set a Template (be it a ControlTemplate, DataTemplate, or ItemsPanelTemplate) using a Resource object, or even better (for extensibility reasons), using a Style object.

So, instead of:

<Window>
    <Control …>
        <Control.Template>
            <ControlTemplate>
                …
            </ControlTemplate>
        </Control.Template>
        …
    </Control>
</Window>

…to avoid a suite of issues that may happen with WPF (especially if you are a control author, using OnApplyTemplate, template parts and things like those, but not only then!), you can use code like this:

<Window>
    <Window.Resources>
        <ControlTemplate x:Key="MyTemplate">
            …
        </ControlTemplate>
       
    </Window.Resources>
    <Control … Template="{StaticResource MyTemplate}">
        …
    </Control>
</Window>

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in Computers and Internet. Bookmark the permalink.

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s