我有以下ListView和ListView.ItemTemplate:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanelName="stackPanel" Orientation="Horizontal">
<TextBoxName="textBoxOrg"
Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingOrgText}"
IsReadOnly="True"/>
<TextBoxName="textBoxNew"
Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingNewText}"
AcceptsReturn="True"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
以下ListViewItemStyle
<Style TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightGoldenrodYellow" />
</Trigger>
</Style.Triggers>
</Style>
我想更改所选项目的默认“蓝色”背景颜色,但在使用上述代码时,选择项目时它不会更改为“LightGoldenrodYellow”。
我应该如何修复代码以使其正常工作?
答案 0 :(得分:0)
您需要自定义ListViewItem的ControlTemplate。否则它会覆盖(不使用)您定义的背景触发器。这是自定义的默认模板(我使用一个名为stylesnooper的有用的小工具来获取模板http://wpfwonderland.wordpress.com/2007/01/02/wpf-tools-stylesnooper/):
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<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">
<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.Value>
<s:Boolean>
True</s:Boolean>
</Trigger.Value>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelected">
<Condition.Value>
<s:Boolean>
True</s:Boolean>
</Condition.Value>
</Condition>
<Condition Property="Selector.IsSelectionActive">
<Condition.Value>
<s:Boolean>
False</s:Boolean>
</Condition.Value>
</Condition>
</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">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>
False</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
答案 1 :(得分:0)
WPF博士有一篇关于ItemsControls的精彩文章,名为(ItemsControl: A to Z),读过I和L
答案 2 :(得分:0)
我终于这样做了:
<Style x:Key="myListboxStyle">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#CCFFFFFF" />
</Style.Resources>
</Style>
它完美无缺。 谢谢你们所有人。