Wpf 鼠标悬停时更改扩展器背景
我想在鼠标悬停时将扩展器的背景色更改为较浅的颜色。所以我想我使用了一个触发器和一个转换器来将背景颜色转换成鼠标上的一种更亮的颜色。我一开始很简单,只实现了触发器。这是有效的:Wpf 鼠标悬停时更改扩展器背景,wpf,xaml,Wpf,Xaml,我想在鼠标悬停时将扩展器的背景色更改为较浅的颜色。所以我想我使用了一个触发器和一个转换器来将背景颜色转换成鼠标上的一种更亮的颜色。我一开始很简单,只实现了触发器。这是有效的: <StackPanel> <StackPanel.Resources> <Style TargetType="{x:Type Expander}"> <Style.Triggers> <Tr
<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 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>
当我添加颜色切换停止工作且扩展器始终为黄色时。为什么会发生这种情况?我如何实现我的目标
编辑:在我的最后一个应用程序中,我将有一个带有多个扩展器的itemscontrol,这些扩展器的背景颜色被数据绑定到我的视图模型中,因此我认为我无法在样式中设置颜色
<ItemsControl ItemsSource="{Binding TestStepDescriptions}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type localVMsTestEngines:AutomaticTestStepDescription}">
<Expander Background="{Binding StepStatus, Converter={StaticResource StepStatusToColorConverter}}">
问候,,
如果使用触发器,则无法直接在扩展器中设置该颜色 相反,你必须这样做:
<Expander Header="Header" Background="Yellow">
<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>
用法
是的,我知道我可以这样做,这是我的第一个示例,但这在我的应用程序中不起作用。我对这个问题作了修改以澄清。对不起,不清楚。当然。您也可以这样做:
可能是的,但似乎不清楚的是您想绑定的部分。无法从资源中的样式绑定到viewmodel,或者可以吗?我将xaml添加到问题的编辑中。@JefPatat更新了答案