我想更改所选背景并让它显示带圆角的渐变。我搜索了Google,发现有些人确实通过覆盖默认颜色来更改所选颜色。有什么方法可以做到这一点吗?我在想是否有任何方法可以在选择项目时显示圆形角落边框作为背景?
答案 0 :(得分:8)
这是ListBoxItem的默认样式(我们想要更改它)。如果您通过右键单击对象和时间轴控件中的listboxitem来使用Expression Blend 4,则可以“检索”此样式。
<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="IsSelected" Value="true">
<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.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>
让我们拉出一些重要的部分,以便你自己学会这样做。
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
这是Style声明的开始。我们给它一个x:Key,因此可以从资源字典中检索它,我们为ListBoxItem设置了TargetType。
现在,我们想要寻找我们想要改变的风格部分。在这种情况下,我们将继续寻找新ControlTemplate上MultiTrigger样式的一部分。
<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>
此MultiTrigger需要2个属性才能匹配这些值才能激活。此触发器在激活时,会将背景颜色更改为Value =“...”,将前景颜色更改为Value =“...”。为了获得渐变背景,我们需要将Background Value =“...”中的值更改为不同的画笔。让我们创建一个快速的小渐变画笔(也非常多彩!)
<LinearGradientBrush x:Key="GradientBrush" StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1.0" />
</LinearGradientBrush>
现在让我们将其应用于此触发器的背景。
<Setter Property="Background" TargetName="Bd" Value="{StaticResource GradientBrush}"/>
现在,当此样式应用于ListBoxItem,并且ListBoxItem IsSelected = True(并且Selector.IsSelectionActive = false)时,您将在listboxitem上看到渐变背景。
现在,你也想要圆角。如果我们走到ControlTemplate的顶部,我们将看到边界声明。
<Border x:Name="Bd"
在该声明中,我们想要添加一个CornerRadius属性来获取ListBoxItem上的圆角。
CornerRadius="5"
现在,你应该能够创建一个角半径,线性渐变背景listboxitem。我希望你能够自己从这里继续。
答案 1 :(得分:1)
我的博客上有一个例子here。它会覆盖ControlTemplate及其使用的颜色。