新的问题出来了,我获得了列表框没有蓝色边框
<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 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>
也许有人会利用这个声明的ControlTemplate。
让您的回复更加精确。 – Maximus
克里斯W反应完成我的。 – Tico