Reputation: 2799
I want to change the background color of ListBoxItem
After search I decide to use this
<ListBox>
<ListBox.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Blue" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Blue" />
</ListBox.Resources>
<TextBlock>fdfsdf1</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf5</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf4</TextBlock>
</ListBox>
When a listboxitem is focused, the background is blue as expected, but when the selected listboxitem loses focus, the background turns gray. How can I make the background remain blue when it loses focus?
Upvotes: 6
Views: 27285
Reputation: 556
I'm using MaterialDesignThemes and Caliburn Micro to make a ListBox that can have multiple selections. None of these other answers worked. What did work for me was in the ViewModel.xaml:
<ListBox Padding="2" Margin="5" Grid.Column="1" Grid.Row="4" Name="Field1Items" SelectionMode="Multiple" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="Background" Value="{Binding ListBoxItemBackground}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Then having this class to encapsulate all my items:
public class MultiSelectItem : PropertyChangedBase
{
public MultiSelectItem (string name)
{
this.Name = name;
_isSelected = false;
}
public string Name { get; set; }
bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
NotifyOfPropertyChange(() => IsSelected);
NotifyOfPropertyChange(() => ListBoxItemBackground);
}
}
public string ListBoxItemBackground
{
get
{
if (IsSelected)
return "#e0e0e0";
return "Transparent";
}
}
public override string ToString()
{
return Name;
}
}
and in the ViewModelClass:
public BindableCollection<MultiSelectItem> Field1Items
{
get
{
return _field1Items;
}
}
Upvotes: 0
Reputation: 118
This is what I used to select the color for the active/inactive items of a ListBox:
<ListBox Name="lbExample" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<...>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Resources>
<!-- Background of selected item when not focussed -->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="AntiqueWhite" />
</Style>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen" />
</ListBox.Resources>
</ListBox>
Upvotes: 0
Reputation: 43636
if you mean just when its selected but inactive try InactiveSelectionHighlightBrushKey
<ListBox.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Blue" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Blue" />
</ListBox.Resources>
Upvotes: 14
Reputation: 5691
if you think the system color keys are not working for you then you can force it by creating new style for ListboxItems as like below.
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Silver"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
Upvotes: 1
Reputation: 2522
Try this
<ListBox>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Blue" />
</Style>
</ListBox.Resources>
<TextBlock>fdfsdf1</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf5</TextBlock>
<TextBlock>fdfsdf3</TextBlock>
<TextBlock>fdfsdf4</TextBlock>
</ListBox>
Upvotes: 7