Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: how to style a class like in css?

Tags:

c#

wpf

xaml

Let's say I have a UserControl with 4 Borders:

<Border />
<Border />
<Border />
<Border />

Now in my Resources I can go:

<Style TargetType="{x:Type Border}">
  ... change some properties here
</Style>

Now this is all good, but it will target all borders in my UserControl. But what if I just want to target a subset of them?

I'd like to go:

<Border Class="Type1" />
<Border Class="Type1" />
<Border />
<Border />

And then go:

<Style TargetType="{x:Type Border}" TargetClass="Type1">
  ... change some properties here
</Style>

But this obviously doesn't exist, is there some other way I can achieve what I'm after? Thanks

like image 499
Shai UI Avatar asked Mar 03 '11 21:03

Shai UI


People also ask

Can I use CSS in WPF?

The only concept for which there really is no correspondent in WPF is CSS class. This can easily be introduced via an attached property.

Can you use CSS with XAML?

Currently, all of the styling that's possible with XAML styling cannot be performed with CSS. However, XAML styles can be used to supplement CSS for properties that are currently unsupported by Xamarin.

How do I apply multiple styles in WPF?

If you change the TargetType in the second style (in first set of xaml above) to ButtonBase , the two Styles do not get applied. However, check out the following xaml below to get around that restriction. Basically, it means you need to give the Style a key and reference it with that key.


2 Answers

Though the syntax isn't quite as clean as in CSS, it is a lot more specific.

To build on your example, what you're looking for is:

<Border Style="{StaticResource Type1}" />
<Border Style="{StaticResource Type1}" />
<Border />
<Border />

And then go:

<Style TargetType="{x:Type Border}" x:Key="Type1">
  ... change some properties here
</Style>

Remember that WPF styles don't actually cascade like CSS does.

A more detailed styling reference: https://web.archive.org/web/20141210000517/http://dotnetslackers.com/articles/wpf/StylesResourcesAndControlTemplatesInWPF.aspx

like image 90
Matt DeKrey Avatar answered Oct 05 '22 01:10

Matt DeKrey


Something that I find most people are not aware of is WPF's ability to nest Styles within Style.Resources. For example:

<!-- Define a new style for Borders called InfoBox, that will have a red background, 
     and further override all buttons within it to have Yellow Text.  An extra style,
     "Strawberry" is also defined, that lets specific buttons be selected to be styled
     as Green FG on DarkRed BG -->
<Style TargetType="{x:Type Border}" x:Key="InfoBox">
  <Setter Property="Background" Value="Red"/>
  <Style.Resources>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Foreground" Value="DarkYellow"/>
    </Style>
    <Style TargetType="{x:Type Button}" x:Key="Strawberry">
      <Setter Property="Foreground" Value="Green"/>
      <Setter Property="Background" Value="DarkRed"/>
    </Style>
  </Style.Resources>
</Style>

...

<Border Style="{DynamicResource InfoBox}">
   <StackPanel>
     <Button Content="I am a banana!"/>
     <Button Style="{DynamicResource Strawberry}" Content="I am red!"/>
   </StackPanel>
</Border>

While not exactly the same as CSS (There isn't much support for standard pseudo-selectors), this gives you a huge amount of power and flexibility. Couple this with skillful use of ItemsControls and you can do some great things.

like image 45
Armentage Avatar answered Oct 04 '22 23:10

Armentage