Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location based styling in WPF possible?

In CSS it is possible to style HTML elements based on their location in the element tree:

div#container div#menu a

I'd like to do the same in WPF, so that only links in a menu section are styled.

  • Question #1: One way or the other, is the above also possible in WPF?

I was thinking about the following for Separators nested in a StatusBar:

<Style TargetType="{x:Type StatusBar}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Padding" Value="0,0,20,0" />
    <Style.Resources>
        <Style TargetType="{x:Type Separator}">
            <Setter Property="Width" Value="20" />
            <Setter Property="Background" Value="Green" />
        </Style>
    </Style.Resources>
</Style>

This piece of XAML is included in a resource dictionary. The StatusBar shows up with a transparent background and correct padding. However, the green separator unfortunately doesn't display. It just shows the default gray 1px wide bar.

  • Question #2: If this is the correct solution, anyone knows why this doesn't work?

Thanks in advance.

like image 495
Herman Cordes Avatar asked Jul 14 '11 13:07

Herman Cordes


1 Answers

Stumbled upon an answer myself. The code I mentioned is correct for every case, except the Separator. The Separator element should be styled with a specific key, detailed described here: http://devlicio.us/blogs/christopher_bennage/archive/2008/06/19/styling-separators-in-wpf.aspx.

like image 106
Herman Cordes Avatar answered Sep 23 '22 09:09

Herman Cordes