Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


Style inheritance

A style in WPF can base on another style. This allows you to specify a base style that sets common properties and derive from it for specialized controls.

 
<Style x:Key="baseStyle" TargetType="TextBlock">
  <Setter Property="FontSize" Value="12" />
  <Setter Property="Background" Value="Orange" />
</Style>
 
 
<Style x:Key="boldStyle" BasedOn="{StaticResource baseStyle}" TargetType="TextBlock">
  <Setter Property="FontWeight" Value="Bold" />
</Style>
 
 




Last modified: 2009-11-12 11:26:14
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Di
Commented on 25.February 2009
Just want i was wondering about...helpful! I like your simple and concise explanations about WPF throughout the site. Good professional looking site!
santhosha k r
Commented on 21.April 2009
Nice thing.
robotix1986
Commented on 14.May 2009
yeah... quite helpful
jlm
Commented on 5.June 2009
Nice and simple, but where does these blocks go?
Giovanni
Commented on 11.November 2009
A Style needs a TargetType, so the example is incomplete. Should be (for example):

<Style x:Key="baseStyle" TargetType="{x:Type TextBox}"
Sergey Malyan
Commented on 26.May 2010
Suggesting to inherit the original Style from "default" TextBlock style.
Like this:
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
mmasweerw
Commented on 30.November 2010
A Style needs a TargetType, so the example is incomplete. Should be (for example):

<Style x:Key=\"baseStyle\" TargetType=\"{x:Type TextBox}\"
Tinoy Malayil
Commented on 8.January 2011
Nice Explanation..

Name
E-Mail (optional)
Comment