2016-07-15 89 views
1

我想将扩展器背景颜色更改为鼠标悬停时更轻的变体。所以我想我使用触发器和转换器将背景颜色转换为鼠标上的较亮变体。我开始简单,只实现触发器。这工作:更改鼠标悬停时的扩展器背景

<StackPanel> 
    <StackPanel.Resources> 
     <Style TargetType="{x:Type Expander}"> 
      <Style.Triggers> 
       <Trigger Property="Control.IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </StackPanel.Resources> 

    <Expander Header="Header"> 
     <StackPanel> 
      <TextBox Background="Transparent">Content</TextBox> 
     </StackPanel> 
    </Expander> 

</StackPanel> 

所以我可以实现我想要的。但我希望能够设置颜色上像这样的扩展:

<Expander Header="Header" Background="Yellow"> 

那一刻我补充一点的颜色切换停止工作,且扩张始终是黄色的。为什么会发生这种情况,我该如何实现目标?

编辑:在我的最终应用程序我将有一个itemscontrol与多个扩展器的背景颜色是数据绑定到我的视图模型,所以我认为我不能设置颜色的风格。

<ItemsControl ItemsSource="{Binding TestStepDescriptions}"> 
    <ItemsControl.Resources> 
     <DataTemplate DataType="{x:Type localVMsTestEngines:AutomaticTestStepDescription}"> 
      <Expander Background="{Binding StepStatus, Converter={StaticResource StepStatusToColorConverter}}"> 

问候, 杰夫

回答

1

如果使用触发器不能设置颜色的扩展直接。

相反,你必须做这样的:

<ItemsControl ItemsSource="{Binding TestStepDescriptions}"> 
    <ItemsControl.Resources> 
     <DataTemplate DataType="{x:Type localVMsTestEngines:AutomaticTestStepDescription}"> 
      <Expander > 
<Expander.Style> 
    <Style TargetType="{x:Type Expander}"> 
    <Setter Property="Background" Value="{Binding StepStatus, Converter={StaticResource StepStatusToColorConverter}}"/> 
       <Style.Triggers> 
        <Trigger Property="Control.IsMouseOver" Value="True"> 
         <Setter Property="Background" Value="Red"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
</Expander.Style> 
</Expander> 

使用 <Expander Header="Header">

+0

是的,我知道我能做到这样,那是我的第一个例子,但是这是行不通的在我的应用程序。我对这个问题进行了编辑以澄清。对不起,缺乏清晰度。 –

+0

当然。你也可以这样做:'' – lokusking

+0

可能是的,但它是WhereEverYouWantToBind部分似乎不清楚。我无法从资源中的样式绑定到视图模型,或者我可以吗?我在问题中将xaml添加到了我的编辑中。 –