Click here to Skip to main content
15,076,130 members
Please Sign up or sign in to vote.
3.10/5 (4 votes)
See more:
I wanted to match my ListViewItem style my my treeview and listbox items, but I can't get the style to take. I've got the following XAML for the style. I set the colors to black in the triggers so that it would be real obvious if I got it to work. I also only included one of the triggers in the interest of brevity. FWIW, this style is in a resource dictionary that contains style overrides for listboxitem and treeviewitem, and both of those seem to work fine.

I also tried adding a setter for OverridesDefaultStyle to True, but that had no effect.

What am I missing?

<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="2,0" />
    <Setter Property="Template"> 
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="outerBorder" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                        <Border x:Name="innerBorder" 
                                Background="{TemplateBinding Background}" 
                                Padding="{TemplateBinding Padding}" 
                                SnapsToDevicePixels="true" />
                        <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="outerBorder" Property="BorderBrush" Value="Black" />
                        <Setter TargetName="innerBorder" Property="Background" Value="Black" />
                        <Setter TargetName="innerBorder" Property="BorderBrush" Value="Black" />
Updated 13-Jan-16 3:43am
Kenneth Haugland 13-Jan-16 10:00am
Not gonna fix anything, but can't you just type:
instead of
x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}"
they are equivalent, or at least I think so:
#realJSOP 13-Jan-16 10:03am
Yeah I know. My problem is that the style override is being ignored (I even tried changing it to the way you have it on the off chance that it would make a diff).
Kenneth Haugland 13-Jan-16 10:14am
Overriding default styles seems to be difficult, although there might be a nasty hack for it:
johannesnestler 13-Jan-16 10:58am
I think you mix up styles with templates here. It's easy to extend and overwrite styles, because there is a basedon attribute you can use (and also override "Default styles", these are styles with no name, but just the type given - so it will apply to all elements (Downstream) of that type without explicitly assigning it. Templates have to be recreated and can't be based on each other.... Confusion may come from the fact, that normally new templates are set through styles...
Richard Deeming 13-Jan-16 10:15am
Are you sure your resource dictionary is being included properly? I've just tried your style in Kaxaml[^], defined directly in the page resources, and it seems to work.
#realJSOP 13-Jan-16 10:24am
Well, the listboxitem and treeview item styles are in the same file, and they're working. I just added comboboxitem and it ain't workin either...
#realJSOP 13-Jan-16 10:51am
And no search engines are displaying - at all - so I can't even continue my search for a solution... I hate having to work on DoD networks...

I figured it out. It was a "Stupid Programmer Trick" (tm). I was only loading the resource dictionary in one window. When I changed it to merged dictionaries in the app.xaml, everything fell into place. :)
#realJSOP 13-Jan-16 12:16pm
Wow. I see the f*cktards are still using the site. Both my question and this solution were down-voted. F*ckin amazing... Don't get me wrong, I don't give a f*ck about the points, but some people really need to grow the f*ck up.
johannesnestler 13-Jan-16 12:24pm
great you solved it!
Kenneth Haugland 13-Jan-16 18:32pm
Yeah, it's been like that forever.
I verified your Style in a new WPF Project. It works. So your style is not the Problem. I think you set another style later or don't include the style at all (unlikely because you said your other styles from the same style are working). So if you use VS 2015 you could inspect the visual structure during debug and see if (or wich) style got applied. But I would just look for a "later" ListViewItem-Style - I think you overwrite it somewhere.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900