【问题标题】:Change expander background on mouse hover在鼠标悬停时更改扩展器背景
【发布时间】:2016-07-15 20:33:06
【问题描述】:

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

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

问候, 杰夫

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

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

    相反,您必须这样做:

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

    用法 &lt;Expander Header="Header"&gt;

    【讨论】:

    • 是的,我知道我可以这样做,这是我的第一个示例,但这在我的应用程序中不起作用。我对问题进行了编辑以澄清。抱歉,不清楚。
    • 当然。你也可以这样做:&lt;Setter Property="Background" Value="{Binding WhereEverYouWantToBind}"/&gt;
    • 可能是的,但似乎不清楚的是WhereEverYouWantToBind 部分。我无法从资源中的样式绑定到我的视图模型,或者可以吗?我将 xaml 添加到问题中的编辑中。
    • @JefPatat 更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多