当前位置:   article > 正文

WPF中listbox点击时,整个背景变成蓝色,怎么让整个背景不变色,只是让单个模块获取焦点_wpf listbox 点击修改其背景色

wpf listbox 点击修改其背景色

http://bbs.csdn.net/topics/390378818


XML/HTML code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
< Window
     xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class = "listbox.MainWindow"
     x:Name = "Window"
     Title = "MainWindow"
     Width = "640"  Height = "480"  Loaded = "Window_Loaded" >
     < Window.Resources >
         < Style  x:Key = "ListBoxItemStyle1"  TargetType = "{x:Type ListBoxItem}" >
             < Setter  Property = "Background"  Value = "Transparent" />
             < Setter  Property = "HorizontalContentAlignment"  Value = "{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
             < Setter  Property = "VerticalContentAlignment"  Value = "{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
             < Setter  Property = "Padding"  Value = "2,0,0,0" />
             < Setter  Property = "Template" >
                 < Setter.Value >
                     < ControlTemplate  TargetType = "{x:Type ListBoxItem}" >
                         < Border  x:Name = "Bd"  BorderBrush = "{TemplateBinding BorderBrush}"  BorderThickness = "{TemplateBinding BorderThickness}"  Background = "{TemplateBinding Background}"  Padding = "{TemplateBinding Padding}"  SnapsToDevicePixels = "true" >
                             < ContentPresenter  HorizontalAlignment = "{TemplateBinding HorizontalContentAlignment}"  SnapsToDevicePixels = "{TemplateBinding SnapsToDevicePixels}"  VerticalAlignment = "{TemplateBinding VerticalContentAlignment}" />
                         </ Border >
                         < ControlTemplate.Triggers >
                             < Trigger  Property = "IsMouseOver"  Value = "True" >
                                 < Setter  Property = "Background"  TargetName = "Bd" >
                                     < Setter.Value >
                                         < LinearGradientBrush  EndPoint = "0.5,1"  StartPoint = "0.5,0" >
                                             < GradientStop  Color = "#8200958B"  Offset = "1" />
                                             < GradientStop  Color = "#7500FFED"  Offset = "0" />
                                         </ LinearGradientBrush >
                                     </ Setter.Value >
                                 </ Setter >
                             </ Trigger >
                             < Trigger  Property = "IsSelected"  Value = "true" >
                                 < Setter  Property = "Foreground"  Value = "{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                             </ Trigger >
                             < MultiTrigger >
                                 < MultiTrigger.Conditions >
                                     < Condition  Property = "IsSelected"  Value = "true" />
                                     < Condition  Property = "Selector.IsSelectionActive"  Value = "false" />
                                 </ MultiTrigger.Conditions >
                                 < Setter  Property = "Background"  TargetName = "Bd"  Value = "{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                                 < Setter  Property = "Foreground"  Value = "{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                             </ MultiTrigger >
                             < Trigger  Property = "IsEnabled"  Value = "false" >
                                 < Setter  Property = "Foreground"  Value = "{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                             </ Trigger >
                         </ ControlTemplate.Triggers >
                     </ ControlTemplate >
                 </ Setter.Value >
             </ Setter >
         </ Style >
     </ Window.Resources >
 
     < Grid  x:Name = "LayoutRoot" >
         < ListBox  HorizontalAlignment = "Left"  Margin = "128,8,0,24"  Width = "172"  ItemContainerStyle = "{DynamicResource ListBoxItemStyle1}"  Name = "listbox1"  FontSize = "16"  />
     </ Grid >
</ Window >


把IsSelected触发器下面的改变背景色属性删掉
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 这个删掉就可以 

如果发现选中的时候变成空白,可以把前景色设置成不是白色的颜色 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/493484
推荐阅读
相关标签
  

闽ICP备14008679号