在鼠标悬停时更改扩展器背景



我想在鼠标悬停时将扩展器的背景颜色更改为较浅的变体。所以我想我使用触发器和转换器在鼠标悬停时将背景颜色转换为较浅的变体。我开始很简单,只实现触发器。那行得通:

<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}}">

问候杰夫

如果使用触发器,则无法直接在扩展器中设置该颜色。

相反,您必须这样做:

<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">

最新更新