数据模板、样式、触发器
我有一个
,其中包含自定义
和
:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="5">
<Image Source="{Binding Picture}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
现在当我选择时ListBoxItem 因蓝色行选择而变得丑陋。我想改变它。我只想为边框的背景着色,而不是其他颜色。我还想更改 MouseOver
行为。我已经尝试过触发器,但是 ContentPresenter
没有 Background 属性。
UPD:
嗯,我已经成功更改了 MouseEnter
和 MouseLeave
的背景:
<EventTrigger RoutedEvent="Border.MouseEnter">
<BeginStoryboard>
<Storyboard >
<ColorAnimation Storyboard.TargetProperty="Background.Color"
To="LightBlue" Duration="0:0:0.03"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
但在选择项目时仍然无法更改Background
。我正在尝试:
<Trigger Property="ListBoxItem.IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
不起作用
I have a <ListBox>
with custom <ListBox.ItemTemplate>
and <DataTemplate>
in it:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="5">
<Image Source="{Binding Picture}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now when I'm choosing the ListBoxItem
it gets ugly with blue colored row selection. I'd like to change it. I want to color only border's background and nothing else. Also I want to change MouseOver
behavior. I've tried trough triggers, but ContentPresenter
doesn't have Background property.
UPD:
Well, I've managed to change the background on MouseEnter
and MouseLeave
:
<EventTrigger RoutedEvent="Border.MouseEnter">
<BeginStoryboard>
<Storyboard >
<ColorAnimation Storyboard.TargetProperty="Background.Color"
To="LightBlue" Duration="0:0:0.03"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
But still can't change the Background
when item's selected. I'm trying through:
<Trigger Property="ListBoxItem.IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
Doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要查找的颜色位于 ListBoxItem 模板内的两个触发器中,而不是 ItemTemplate 中。要更改此设置,您需要编辑列表框的 ItemContainerStyle。这是可以用作起点的默认值:
The coloring you're looking for is in two Triggers inside the template for ListBoxItem, not the ItemTemplate. To change this you need to edit the ItemContainerStyle for the ListBox. This is the default that can be used as a starting point:
您可以使用触发器来做到这一点。我在我的一个项目中有这样的内容:
虽然它不会改变边框颜色,但它显示了如何更改背景。所以,也许尝试将其设置为空。此触发器是自定义样式的一部分,其中使用 IsMouseOver 属性实现跟踪。
华泰
You can do this by using triggers. I have in one of my projects something like this:
Though it doesn't change Border color, it shows how to change the Background. So, maybe try to set this to null. This triggers are part of a custom Style in which a tracking is achieved using IsMouseOver property.
HTH