新问题出来了,我获取了没有蓝色边框的列表框
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter
x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但我已经像这样设置了 ItemContainerStyle
<Style TargetType="ListBoxItem" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource not_mouseover}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource mouseover}"/>
</Trigger>
</Style.Triggers>
</Style>
<ListBox ItemsSource="{Binding lista}" ItemContainerStyle="{StaticResource ContainerStyle}">
在这种情况下,事实证明它不起作用(我的意思是蓝色边框像以前一样出现)。如果我将 ItemTemplate 设置为任何指定的 DateTemplate 它工作正常,但这里不是。你碰巧知道为什么吗?我整理了这个。ListboxItem 只有一种样式
<Style x:Key="item_template" TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Template" Value="{StaticResource control_mouseover}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Template" Value="{StaticResource control_not_mouseover}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<ListBox ItemsSource="{Binding lista}" ItemContainerStyle="{StaticResource item_template}">
</ListBox>
并声明 ControlTemplate 以删除蓝色边框
<ControlTemplate x:Key="control_not_mouseover" TargetType="ListBoxItem">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{StaticResource not_mouseover}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
<ControlTemplate x:Key="control_mouseover" TargetType="ListBoxItem">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{StaticResource mouseover}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</ControlTemplate>
也许有人会利用这一点。