【发布时间】:2018-09-23 20:21:26
【问题描述】:
我正在尝试更改ListBox,以便当我将鼠标悬停在它上面时,它不再显示默认的IsMouseOver style。
根据我的在线搜索,建议的代码看起来像这样;
<ListBox ItemsSource="{Binding ExampleItems}">
...
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Red" />
<Setter Property="BorderBrush" Value="Blue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Gold"/>
<Setter Property="BorderBrush" Value="Purple"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
此代码生成正确的初始着色结果,也就是说,项目以红色Background 和蓝色Border 显示;
但IsMouseOver 触发器仍显示默认样式;
我知道颜色很糟糕,这只是为了演示目的。关于我做错了什么有什么建议吗?
编辑:
正如已经回答的那样,我找到了ControlTemplate(通过Properties-Misc 面板),但是我现在不确定如何设置我在Xaml 中看不到的ListBoxItem 的ControlTemplate。一旦程序通过ItemSource 绑定运行,就会创建ListBoxItems,有没有办法为数据绑定ListBoxItems 的ControlTemplate 设置ListBox?
谢谢!
【问题讨论】: