Afn*_*hir 3 c# wpf listbox selecteditem background-color
我想更改所选背景并让它显示带圆角的渐变.我搜索了Google,发现有些人确实通过覆盖默认颜色来更改所选颜色.有什么方法可以做到这一点吗?我在想有没有办法在选择项目时显示圆形角落边框作为背景?
这是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>
Run Code Online (Sandbox Code Playgroud)
让我们提取一些重要的部分,以便您自己学会这样做.
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
Run Code Online (Sandbox Code Playgroud)
这是Style声明的开始.我们给它了ax: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>
Run Code Online (Sandbox Code Playgroud)
此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>
Run Code Online (Sandbox Code Playgroud)
现在让我们将其应用于此触发器的背景.
<Setter Property="Background" TargetName="Bd" Value="{StaticResource GradientBrush}"/>
Run Code Online (Sandbox Code Playgroud)
现在,当此样式应用于ListBoxItem,并且ListBoxItem IsSelected = True(和Selector.IsSelectionActive = false)时,您将在listboxitem上看到渐变背景.
现在,你也想要圆角.如果我们走到ControlTemplate的顶部,我们将看到边界声明.
<Border x:Name="Bd"
Run Code Online (Sandbox Code Playgroud)
在该声明中,我们想要添加一个CornerRadius属性来获取ListBoxItem上的圆角.
CornerRadius="5"
Run Code Online (Sandbox Code Playgroud)
而现在,您应该能够创建一个角半径,线性渐变背景listboxitem.我希望你能够自己从这里继续.
| 归档时间: |
|
| 查看次数: |
1594 次 |
| 最近记录: |