WPF ListBox,如何隐藏边框并更改所选项目的背景色? [英] WPF ListBox, how to hide border and change selected item background color?
问题描述
我想隐藏ListBox的边框,并使选定项目的背景与未选定项目相同.
I'd like to hide the border of ListBox, and make background of selected item the same as unselected ones.
我该怎么做?
推荐答案
要隐藏边框,请使用
<ListBox BorderThickness="0"/>
如果不想选择,请使用ItemsControl
代替ListBox
.
If you don't want to have a selection, use an ItemsControl
instead of the ListBox
.
以下代码隐藏了ListBox的边框,并且始终在项目上显示白色背景(如果它是通过ItemsSource
-property生成的).
The following code hides the border around the ListBox and does always show a white background on the item (if its generated through the ItemsSource
-property).
<ListBox BorderThickness="0" HorizontalContentAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="White">
<ContentPresenter Content="{Binding}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果使用ListViewItem-instances,则必须在其中更改背景.
If you use ListViewItem-instances, you must change the background there.
更新
<ListBox BorderThickness="0" HorizontalContentAlignment="Stretch" >
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
</Style.Resources>
</Style>
</ListBox.Resources>
</ListBox>
这也应与ListBoxItem-instances一起使用,并且是IMO较少的解决方法".
This should work also with ListBoxItem-instances and is IMO less "work-around".
这篇关于WPF ListBox,如何隐藏边框并更改所选项目的背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!