我想更改选定的背景并使其显示具有圆角的渐变。我搜索了Google,发现有些人通过覆盖默认颜色来改变所选的颜色。有什么办法可以做到这一点?我在想,有什么办法可以在选择一个项目时显示一个圆形的边框作为背景?更改选定的颜色列表框
3
A
回答
8
这是ListBoxItem的默认样式(这是我们想要更改的)。如果通过右键单击Objects和Timelines控件中的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}">
这是样式声明的开始。我们给它一个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
我在我的博客here上有一个示例。它覆盖ControlTemplate及其使用的颜色。
相关问题
- 1. 更改选定列表框项目的背景颜色
- 2. 如何更改asp.net中列表框的选定颜色
- 3. 如何更改选定列的颜色?
- 4. 列表框选定的文本颜色
- 5. 更改复选框的颜色边框
- 6. 更改列表框中所选项目的颜色
- 7. 如何更改Xul列表框中所选项目的颜色?
- 8. 如何更改列表框中未选中的项目颜色
- 9. 更改复选框边框颜色
- 10. 更改绑定的LongListSelector或列表框的项目颜色
- 11. 停止选择框从更改选定的选项颜色
- 12. 更改列表中选定区域的颜色?
- 13. 使用knockout.js更改多个列表框中的颜色或选定项目
- 14. 更改tilelist上选择的背景颜色和边框颜色?
- 15. WP7 - 更改multiselectlist复选框颜色
- 16. jquery移动更改复选框颜色
- 17. Javascript:onClick复选框更改div颜色
- 18. 更改三态复选框上的红色框的颜色
- 19. 当选择项目时更改列表框中的边框元素的颜色
- 20. Devexpress gridcontrol更改选定的行颜色
- 21. 更改选定文本的颜色Javascript
- 22. 如何更改选定的ListItem颜色
- 23. 表行选择的颜色更改JavaFX
- 24. WPF如何在列表框丢失焦点时更改列表框中选定的项目文本颜色
- 25. 当选中复选框时更改表格列文本的颜色
- 26. 更改颜色表时复选框勾选
- 27. 更新C#中的列表框颜色#
- 28. 根据选定的颜色更改颜色
- 29. Android:更改列表行的颜色
- 30. 更改列表视图行的颜色
哇,很好的答案! – CodeNaked 2011-04-02 00:33:25
Wao这是伟大的迷你toutorial.You是男人。 首先它没有工作,但改变'GradientBrush'name的工作。我认为它有任何方式有一些冲突。 这是一个很好的答案 – 2011-04-02 01:07:45