我试图改变一些元素的颜色,当他们被选中和鼠标移过它们时。在我第一次尝试使用ItemsControl
和一个边框来激活颜色变化时,这会给我鼠标上的颜色变化,但我不确定我需要触发哪个属性来选择它,如下所示:IsFocussed
不正确:我可以选择一个边框吗?或者我该如何避免打破我的列表框继承?
<UserControl x:Class="Crp.CodePlusTeamExplorerSectionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Crp.ViewModels="clr-namespace:Crp.ViewModels"
mc:Ignorable="d"
d:DesignHeight="250" d:DesignWidth="300">
<UserControl.DataContext>
<Crp.ViewModels:RelatedViewsViewModel/>
</UserControl.DataContext>
<Control.Resources>
<Style x:Key="styleWithTrigger" TargetType="Border">
<Setter Property="Background" Value="Transparent" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#3E3E40" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="#007ACC" />
</Trigger>
</Style.Triggers>
</Style>
</Control.Resources>
<ItemsControl Name="RelatedViewsICtl" ItemsSource="{Binding RelatedViews}" MouseDoubleClick="RelatedViewsLB_MouseDoubleClick">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Name="border" VerticalAlignment="Stretch" Height="23" MinHeight="22" Style="{StaticResource styleWithTrigger}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Text="" Margin="1"/>
<Image Source="Resources\Review.png"/>
<TextBlock Text="{Binding Path=Id}" Margin="1"/>
<TextBlock Text="-" Margin="1"/>
<TextBlock Text="{Binding Path=Name}" Margin="1" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
我曾尝试另一种方法是将其转换为一个ListBox
,这似乎是一个更好的办法,但列表框继承了正确的属性停在我的元素,而且我不确定如何修复此问题
:
然后,您可以将样式容器
ListBoxItem
。 “listbox阻止我的元素继承正确的属性” - 请你澄清/详细说明这个声明吗? – ASh您也可以覆盖系统颜色。当我想在全球范围内做东西时,我真的很喜欢它。因为您不再需要使用触发器或担心事件。您只需为给定对象设置SystemColor,并将其“MouseOver”设置为您想要的颜色。 [参考](https://blogs.msdn.microsoft.com/wpf/2010/11/30/systemcolors-reference/) –