Click here to Skip to main content
15,881,776 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?

XML
<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="2,0" />
    <Setter Property="Template"> 
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Grid>
                    <Border x:Name="outerBorder" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            CornerRadius="2" 
                            SnapsToDevicePixels="true">
                        <Grid>
                        <Border x:Name="innerBorder" 
                                Background="{TemplateBinding Background}" 
                                BorderThickness="1" 
                                CornerRadius="1" 
                                Padding="{TemplateBinding Padding}" 
                                SnapsToDevicePixels="true" />
                        <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <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" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter> 
</Style>
Posted
Updated 13-Jan-16 3:43am
v2
Comments
Kenneth Haugland 13-Jan-16 10:00am    
Not gonna fix anything, but can't you just type:
TargetType="ListViewItem"
instead of
x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}"
they are equivalent, or at least I think so:
https://msdn.microsoft.com/fr-fr/library/ms745683(v=VS.85).aspx#styling_targettype
#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:
http://stackoverflow.com/questions/2221187/replace-part-of-default-template-in-wpf/19354023#19354023
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.

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. :)
 
Share this answer
 
Comments
#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.
 
Share this answer
 

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