求教WPF中ListView如何改变选中条背景颜色?

jackson35296 2008-11-08 12:22:42
感觉很简单的一个东西,结果搞了很久没没搞出来。我是想改变ListView选中行的背景颜色,默认是系统的那种深蓝色,很丑,和界面不搭调。(ListView.View为一GridView)
我尝试过的方法:
第一次尝试:
把MSDN上ListView的所有例子都看过了也没找到解决的方法。我是通过设置ListView的ListView.ItemContainerStyle属性,在Style.Triggers里用属性触发器,IsMouseOver的时,ListViewItem的背景颜色都可以改变,但是IsSelected时改变就不行,我确信IsSelected属性确实被触发了,因为前景色可以改变,我想知道难道背景色和前景色之间还有一层?查看MSDN上ListViewItem的控件模板,发现中间也只有一层透明背景的Border而已。又考虑难道是系统进一步处理,把背景变成默认色了?在没有选择时背景色都能反应出来,一选中就深蓝色了,MSDN上找不到答案。
第二次尝试:
在ListViewItem上放置一StackPanel,也是参考了例子,IsMouseOver的时候可以改变StackPanel的颜色,就是当IsFocued时改变就没反应,还是StackPanel原来的背景色。
请各位高手帮忙啊,所有能利用的资源我都利用了,MSDN,百度,CSDN都找得差不多了,没有好的解决办法。主要是为了方便显示数据才用的ListView,里面每隔几秒就刷数据,要不我也不用ListView+GridView的模式。
问题说完了,请大家提供点思路,分数可以追加。还有我想知道Select一行以后,系统都做了点啥事情,那深蓝的系统的默认颜色到底加在了哪个控件上面?
...全文
3906 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
headland 2012-06-27
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/4246795/change-selection-color-of-wpf-listviewitem
wxjing67 2012-06-25
  • 打赏
  • 举报
回复
楼主,这个问题怎么解决的?
一楼说在Resource Dictionary 有一个配置,我查得到背景色的,但是查不到前景色的,希望楼主能给与帮助
波粒 二象性 2012-02-08
  • 打赏
  • 举报
回复
唉,同样的问题,3楼的代码拷进去,XP下选中的触发器还是没用哦,郁闷
  • 打赏
  • 举报
回复
顶下,这个问题挺纠结的。。。。。。。。。。。
wwwyfs1511 2011-03-23
  • 打赏
  • 举报
回复
怎么解决的
zhouyongh 2008-11-11
  • 打赏
  • 举报
回复
http://blogs.msdn.com/llobo/archive/2006/07/17/Tool-to-Examine-WPF-control-styles.aspx

一个小工具,带源代码的,好像也有类似的东东。
用习惯了
jackson35296 2008-11-11
  • 打赏
  • 举报
回复
太感谢了!再问点问题StyleSnooper是啥软件?找不到啊
我在MSDN查的ListViewItem的控件模板为啥就这么点呢?
[code=XAML]
<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
Name="Border"
Padding="2"
SnapsToDevicePixels="true"
Background="Transparent">
<GridViewRowPresenter
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border"
Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground"
Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
[code]
zhouyongh 2008-11-11
  • 打赏
  • 举报
回复
没那么麻烦的,这种事情,用StyleSnooper看一下默认的Style,改一下就可以了。

三种颜色 IsMouseOver = Blue
Select & Focus = Red
Select & UnFocus = Yellow

[code=XAML]
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color=" Red"/>
</Style.Resources>
<Setter Property="Panel.Background" Value="#00FFFFFF"/>
<Setter Property="Control.HorizontalContentAlignment">
<Setter.Value>
<Binding Path="HorizontalContentAlignment"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
</Setter.Value>
</Setter>
<Setter Property="Control.VerticalContentAlignment">
<Setter.Value>
<Binding Path="VerticalContentAlignment"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" />
</Setter.Value>
</Setter>
<Setter Property="Control.Padding" Value="2,0,0,0"/>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
Name="Bd"
SnapsToDevicePixels="True">
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
</Setter.Value>
</Setter>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected" Value="True"/>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="UIElement.IsMouseOver" Value="True">
<Setter Property="Panel.Background" TargetName="Bd" Value="Blue">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListView Margin="48,22,110,0" Name="listView1" Height="100" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridViewColumn Header="12"/>
<GridViewColumn Header="12"/>
<GridViewColumn Header="12"/>
</GridView>
</ListView.View>
<ListViewItem>123</ListViewItem>
<ListViewItem>123</ListViewItem>
<ListViewItem>123</ListViewItem>
</ListView>
<TextBox Height="23" Margin="94,0,64,67" Name="textBox1" VerticalAlignment="Bottom" />
</Grid>
</Window>
[/code]

Hope helps
jackson35296 2008-11-10
  • 打赏
  • 举报
回复
大哥,在不在啊?我QQ是576704258,帮帮忙啊
ms44 2008-11-08
  • 打赏
  • 举报
回复
晕哦。
不需要那么复杂的。

前两天刚配置了一堆的SKIN。
正好有个LISTVIEW在其中。

选中行的背景颜色设置很其他的一样的。
在Resource Dictionary 中同样配置一个就可以么?名字叫什么现在记不起来。

你找找看,还是找不到,星期一到公司再发给你。 。

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧