【发布时间】:2010-07-28 10:19:29
【问题描述】:
我想隐藏 ListBox 的边框,并使选中项的背景与未选中项的背景相同。
我该怎么做?
【问题讨论】:
标签: wpf control-template
我想隐藏 ListBox 的边框,并使选中项的背景与未选中项的背景相同。
我该怎么做?
【问题讨论】:
标签: wpf control-template
要隐藏边框,请使用
<ListBox BorderThickness="0"/>
如果您不想进行选择,请使用 ItemsControl 而不是 ListBox。
以下代码隐藏了 ListBox 周围的边框,并且总是在项目上显示白色背景(如果它是通过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,你必须改变那里的背景。
更新
<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 较少的“解决方法”。
【讨论】: