新的問題出來了,我獲得了列表框沒有藍色邊框
<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