Styles (followup)

Yesterday’s post did not include source, and left fleshing out the styles as an exercise… this quick follow-up will provide a link to the full code and a quick walkthrough of the styles I ended up creating.

The tricky part is that there is not a single base class for the various controls we’re using (e.g., TextBlock, TextBox etc.) and the based on property requires that the target be either the base style class or a class derived from the base style class. A quick check of the Silverlight .chm file reveals the inheritance structure shown in the figure



The dotted line from Control to CheckBox, RadioButton and ListBox is intended to indicate that these three classes inherit indirectly from Control.

With this structure in mind, we can create our styles, starting with the FWEBaseStyle (Framework Element Base) and deriving from that a style for TextBlocks and a second that factors out all the common styles for the remaining four types.


        Value="5" />
        Value="Right" />
        Value="Bottom" />

We’ll derive InputControlStyle from the FWEStyle, providing all we need for three of the four Control types; but Toolbox will need additional properties to handle, e.g., TextWrap

    BasedOn="{StaticResource FWEStyle}">
        Value="Left" />
        Value="Georgia" />
        Value="14" />
        Value="Normal" />

    BasedOn="{StaticResource InputControlStyle}">
        Value="Wrap" />
        Value="120" />
        Value="Auto" />

All that is left is to apply these styles to the controls, which can be done either by dragging the Style onto the control in Blend, or by copying the style property in Visual Studio; for example:

    Style="{StaticResource InputControlStyle}" />

The complete source listing is here

About Jesse Liberty

Jesse Liberty is the Principal Mobile Developer with IFS Core. He has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses, and has been a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Blend, Mini-Tutorial, Tools and Utilities, z Silverlight Archives and tagged , . Bookmark the permalink.

One Response to Styles (followup)

  1. Pingback: DotNetShoutout

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.