And*_*ndy 8 wpf listboxitem background-color
我在设置WPF中HighlightBrushKey的a SelectedItem时遇到了问题Listbox.我的目的是根据给定的布尔值设置Item的颜色,位于代码中.
我试过以下步骤:
实现转换器,检查布尔值并返回正确的颜色.
例:
<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>
Run Code Online (Sandbox Code Playgroud)
这里的问题是Convert方法只被调用一次,但我每次选择一个项目并检查布尔值时都需要调用Converter.像触发器一样,但带有" HighlightBrushKey".
转换器:
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if(currentField == null)
return Brushes.Yellow;
if (currentField.Save)
return Brushes.LightGreen;
else
return Brushes.Yellow;
}
Run Code Online (Sandbox Code Playgroud)我的下一个想法是将" HighlightBrushKey" 设置为" Transparent"并item.Background在代码中手动更改.这里的问题是我的物品变白了,无法看到背景颜色
例:
<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>
Run Code Online (Sandbox Code Playgroud)提前致谢!:)
小智 1
<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>
Run Code Online (Sandbox Code Playgroud)