WPF更改ListboxItem选中时高亮显示颜色
我在WPF中设置WPF更改ListboxItem选中时高亮显示颜色,wpf,background-color,listboxitem,Wpf,Background Color,Listboxitem,我在WPF中设置列表框的SelectedItem的高亮笔刷键时遇到问题。我的意图是根据代码中给定的布尔值设置项目的颜色 我尝试了以下步骤: 实现一个转换器,检查布尔值并返回正确的颜色 例如: <ribbon:RibbonWindow.Resources> <l:WindowControl x:Key="ListBoxItemBackgroundConverter" /> <Style x:Key="listBoxStyle" TargetType="{
列表框的SelectedItem的高亮笔刷键时遇到问题。我的意图是根据代码中给定的布尔值设置项目的颜色
我尝试了以下步骤:
- 实现一个转换器,检查布尔值并返回正确的颜色
例如:
<ribbon:RibbonWindow.Resources>
<l:WindowControl x:Key="ListBoxItemBackgroundConverter" />
<Style x:Key="listBoxStyle" TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={x:Static SystemColors.HighlightBrushKey}, Converter={StaticResource ListBoxItemBackgroundConverter}}"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{Binding Source={x:Static SystemColors.ControlBrushKey}, Converter={StaticResource ListBoxItemBackgroundConverter}}"/>
</Style.Resources>
</Style>
</ribbon:RibbonWindow.Resources>
- 我的下一个想法是将“
HighlightBrushKey
”设置为“Transparent
”,并在code中手动更改项。这里的问题是,我的物品变成了白色,背景色看不见
例如:
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
提前感谢!:)
如果您希望在选择listboxitem或将鼠标悬停在其上时禁用突出显示,可以使用以下代码
<Style TargetType="ListBoxItem" x:Key="ListBoxItemStyle">
<Setter Property="IsSelected" Value="{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"/>
第一个问题很好,Andy,结构良好,有精确的示例,正好说明了您想要强调的内容+1@Andy转换器中的currentField1是什么?你是怎么把这个放进转换器的?您能否尝试以提供的样式绑定到currentField
(即您的ViewModelProperty)。currentField是一个对象。类名是字段,具有名为“Save”的布尔属性。我怎样才能在XAML中绑定到它?听起来你需要一个多值转换器来检查IsSelected和你的boolthanks以获得答案,但这并不能解决我的问题。我知道这些行,但在颜色(您的示例:color=“Red”)和布尔值currentField.Save(运行时的对象)之间存在依赖关系。如果为false,则颜色应为黄色,如果为true,则颜色应为绿色。这不再适用于在ControlTemplate触发器中使用静态颜色的Windows-8。换个角度考虑。
<Style x:Key="listBoxStyle" TargetType="{x:Type ListBox}">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
</Style.Resources>
</Style>
<ListBox Style="{StaticResource listBoxStyle}">
</ListBox>
<Style TargetType="ListBoxItem" x:Key="ListBoxItemStyle">
<Setter Property="IsSelected" Value="{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}"/>