2017-08-24 15 views
0

我试图改变一些元素的颜色,当他们被选中和鼠标移过它们时。在我第一次尝试使用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,这似乎是一个更好的办法,但列表框继承了正确的属性停在我的元素,而且我不确定如何修复此问题

+0

然后,您可以将样式容器ListBoxItem。 “listbox阻止我的元素继承正确的属性” - 请你澄清/详细说明这个声明吗? – ASh

+0

您也可以覆盖系统颜色。当我想在全球范围内做东西时,我真的很喜欢它。因为您不再需要使用触发器或担心事件。您只需为给定对象设置SystemColor,并将其“MouseOver”设置为您想要的颜色。 [参考](https://blogs.msdn.microsoft.com/wpf/2010/11/30/systemcolors-reference/) –

回答

1

如果你想能够选择一个项目,你应该使用ListBox。 A ListBox是一个专门的ItemsControl,基本上增加了选择项目的能力。外的箱子更有意义使用列表框与选择支持

<ListBox x:Name="RelatedViewsICtl" ItemsSource="{Binding RelatedViews}" MouseDoubleClick="RelatedViewsLB_MouseDoubleClick"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Background" Value="Transparent" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Border Name="border" VerticalAlignment="Stretch" Height="23" MinHeight="22" 
             Background="{TemplateBinding Background}"> 
          <ContentPresenter /> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="#3E3E40" /> 
       </Trigger> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Background" Value="#007ACC" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <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> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <VirtualizingStackPanel Orientation="Vertical"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ListBox> 
+0

这工作的背景,虽然我不得不调整边界删除它的画笔。前景(文本的颜色)我必须手动设置。我看不到如何使它继承正确的颜色。 – Noel

+0

@Noel:请记住投票赞成有用的答案:) – mm8

0

您应该明确地使用列表框,但默认的ListBoxItem模板的定义方式,它将结束骑任何你设定的背景值。这可能是您列表框问题的原因。解释可以在this answer中找到。

简而言之,任何设置为ItemContainerStyle中ListBoxItem的背景属性的内容都将不会使用,因为默认模板触发器会针对该模板中的特定边框而不是控件本身的属性。解决办法是简单地将覆盖整个模板

您可以通过在XAML编辑器中输入快速到达它,选择它并转到属性选项卡。从那里,在杂项 - >模板下,您可以选择"Convert to a new resource"

相关问题