我想隐藏ListBox的边框,并使所选项目的背景与未选项目相同。
我该怎么做?
我该怎么做?
要隐藏边框,请使用
<ListBox BorderThickness="0"/>
如果您不想要选择功能,请使用ItemsControl
而不是ListBox
。
以下代码可以隐藏ListBox
周围的边框,并始终在项上显示白色背景(如果是通过ItemsSource
属性生成的话)。
<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实例,则必须在那里更改背景。
更新
<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实例,并且我认为这种方法更少"折中".