MouseOver删除ListboxItem的蓝色边框
问题描述:
我一直试图删除蓝色框,当鼠标在Listbox中的项目结束,我跑出了想法,也许你会出来任何。先谢谢你。 MouseOver删除ListboxItem的蓝色边框
简单列表框
<ListBox ItemsSource="{Binding Mylist}" />
不幸的是,解决方案如下不起作用
<ListBox ItemsSource="{Binding lista}" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
答
此行为是由控制模板支配。
如果您对XAML熟悉,请右键单击列表框,转至Edit Template -> Edit Copy...
检查Border
标签。
为了帮助你,检查此链接,以及:ListBox Styles and Templates
答
新的问题出来了,我获得了列表框没有蓝色边框
<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。
答
没有x:Key
的样式可用于所有TargetType控件。
例如:
<Style TargetType="Button">
<Setter Property="Background" Value="Green" />
</Style>
将工作每次设定一个新的Button
控制时间。因此,如果您插入Button
而未指定如下样式:<Button/>
它将具有绿色背景,如上所述。
在另一方面:
<Style TargetType="Button" x:Key="myButton">
<Setter Property="Background" Value="Green" />
</Style>
将只Button
控制指定Style
模板工作。
例如:<Button Style="{StaticResource myButton}" />
- >此Button
将具有绿色背景,所有其他按钮将具有默认背景色。
我的建议是:总是在您的样式中设置一个x:Key,以便稍后设置它们。在您的场景中,将放在第一个代码处,并删除稍后声明的样式。它应该工作。
让您的回复更加精确。 – Maximus
克里斯W反应完成我的。 – Tico