我正在使用XAML,当我点击其中一个ListBoxItem时,背景会将颜色更改为控件默认行为的一部分。我试图做的只是改变所选项目的背景颜色,但我不能。
我认为这与focusable
属性有关,而且它会覆盖我的背景。
我的努力(显然这是ListBox
)
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#ff00ff" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
代码运行但背景颜色仍为默认蓝色。
答案 0 :(得分:0)
下面给出的样式是默认样式。你需要改变你想要的二传手。 注释掉了默认的setter。你可以玩它们。我评论了默认的一个并添加了我自己的。现在,我选择的项目显示为黄色,而不是默认的蓝色。
注意:如果您不知道,可以右键单击“文档大纲”中的列表框,并查看“其他模板”中提供的选项。
<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}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" Background="#FFE64747">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" OpacityMask="#FFB02626"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="Yellow"/>
<!--<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<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.InactiveSelectionHighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>-->
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
...
<ListBox ItemContainerStyle="{DynamicResource ListBoxItemStyle1}" >
答案 1 :(得分:0)
我可以建议您采用下一种方法:
<Window x:Class="SoListBoxStyleHelp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<x:Array x:Key="StringArray" Type="system:String">
<system:String>Examle Two</system:String>
<system:String>Examle Three</system:String>
<system:String>Examle Five</system:String>
<system:String>Examle Seven</system:String>
</x:Array>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{StaticResource StringArray}" Background="#00FFFFFF">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="BorderBrush" Value="DimGray"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="Grid">
<Border x:Name="SimpleBackground" Visibility="Visible" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Border>
<Border x:Name="OnSelectedBackground" Visibility="Collapsed" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="Tomato" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Border>
<ContentPresenter x:Name="ContentPresenter"></ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="OnSelectedBackground" Property="Visibility" Value="Visible"></Setter>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="OnSelectedBackground" Property="Visibility" Value="Collapsed"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
的问候,