TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
XAML Coding Standard
Search

STY$002Checked automatically with code checker

Synopsis:Consider introducing implicit styles to avoid duplication of XAML code
Language:XAML
Severity Level:1
Category:Styles


Description:

Sometimes, your XAML needs to define a number of elements of the same type that should all have certain properties set in the same way. In order to avoid duplication of XAML code, you should consider defining a local implicit style to take care of all the commonalities. Implicit styles are one of the most powerful tools that the XAML developer has to increase the uniformity of the UI elements, and to enhance the readability and the maintainability of the XAML code.

The following example illustrates the use of an implicit style based on an existing custom button style that sets the width and height for a collection of buttons.

  <!-- If you do not define an implicit style, each of the properties must be set 
     explicitly for each button, even if, by design, a property should have the
     same value for multiple buttons. -->
<Button x:Name="okButton"
    Content="OK"
    Width="120" Height="38"/>
<Button x:Name="cancelButton"
    Content="Cancel"
    Width="120" Height="38"/>

  <!-- Define an implicit style.
     Note that an implicit style should not have an x:Key. -->
<Style
    BasedOn="{StaticResource MyCustomButtonStyle}"
    TargetType="{x:Type Button}">
    <Setter Property="Width" Value="120"/>
    <Setter Property="Height" Value="38"/>
    </Style>
    ...
<!-- The implicit style will be applied to these buttons without explicitly
     setting it. This means that the OK and the Cancel button will both have
     the width and the height that is specified by the implicit style. -->
<Button x:Name="okButton"
    Content="OK"/>
<Button x:Name="cancelButton"
    Content="Cancel"/>