WPF ListViewItem样式 [英] WPF ListViewItem Style

查看:337
本文介绍了WPF ListViewItem样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将我的ListViewItem样式与我的树视图和列表框项匹配,但我无法获得样式。我的风格有以下XAML。我在触发器中将颜色设置为黑色,这样如果我让它工作就会非常明显。为了简洁起见,我也只包含了一个触发器。 FWIW,这个样式在一个资源字典中,包含了listboxitem和treeviewitem的样式覆盖,这两个似乎都运行正常。



我也尝试将OverridesDefaultStyle的setter添加到True,但这没有效果。



我缺少什么?



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"> 
        <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>

推荐答案

我想通了。这是一个愚蠢的程序员伎俩(tm)。我只在一个窗口中加载资源字典。当我在app.xaml中将其更改为合并的词典时,一切都已落实到位。 :)
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. :)


我在新的WPF项目中验证了你的Style。有用。所以你的风格不是问题。我认为你之后再设置另一种风格或根本不包括风格(不太可能,因为你说你的同一风格的其他风格都有效)。因此,如果您使用VS 2015,您可以在调试期间检查可视化结构,并查看是否应用了(或哪种)样式。但我只想找一个以后的ListViewItem-Style - 我想你会在某处覆盖它。
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.


这篇关于WPF ListViewItem样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆