做到這一點內部容器風格,而不是DataTemplate。即定義您自己的ListBoxItem
風格,並只突出顯示所需內容。例如: -
<Style x:Key="MyStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border
Name="Border"
Margin="0, 0, 0, 1"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<Border Grid.Row="1" BorderThickness="0, 0, 0, 1" BorderBrush="Black"
SnapsToDevicePixels="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然後:
<ListBox ItemContainerStyle="{StaticResource MyStyle}">