<转>Change the Background of a selected ListBox Item


If you select an item in a listbox it gets the default selection color (usually blue) as background. Even if you specify a custom data template. The reason is that the blue background (or gray if the control is not focussed) is drawn outside of the data template. So you have no chance to override it from within the data template.

The color used to draw the blue and gray background are system colors. So the easiest way to get rid of these backgrounds is to locally override the highlight and control brushes of the system colors.

The best way to do this is to create a style for the listbox. Place the style in the resources of a parent element. For e.g. Window.Resources

The following XAML snippet shows how to apply the style to the listbox.

1 <Style x:Key="myListboxStyle">
2 <Style.Resources>
3 <!-- Background of selected item when focussed -->
4 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
5 <!-- Background of selected item when not focussed -->
6 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
7 </Style.Resources>
8 </Style>
优质内容筛选与推荐>>
1、Fireworks快捷键
2、解决移动端的一像素
3、Sql中IN的参数传递问题
4、转:iphone网络编程总结一
5、一个人的旅行 HDU 2066 &&HDU Today HDU 2112


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号