[英]Listbox WPF item background color
我想更改 ListBoxItem 的背景顏色
搜索后我決定使用這個
<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>
當listBoxItem集中時,背景是藍色的,但是當所選的ListBoxItem失去焦點時,背景會變為灰色。 失去焦點時如何使背景保持藍色?
如果您的意思是僅當其處於選中狀態但不活動時,請嘗試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>
嘗試這個
<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>
如果您認為系統顏色鍵不適合您,則可以通過如下所示為ListboxItems創建新樣式來強制使用它。
<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>
這就是我用來為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>
我正在使用 MaterialDesignThemes 和 Caliburn Micro 制作一個可以有多個選擇的 ListBox。 這些其他答案都沒有奏效。 對我有用的是 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>
然后讓這個 class 封裝我所有的項目:
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;
}
}
在 ViewModelClass 中:
public BindableCollection<MultiSelectItem> Field1Items
{
get
{
return _field1Items;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.