如何在 WPF 中更改鼠标悬停时的瓷砖背景? [英] How to change tile background on mouse over in WPF?

查看:24
本文介绍了如何在 WPF 中更改鼠标悬停时的瓷砖背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用本文提供的代码:

在每次绑定时使按钮点击的背景颜色变亮转换器

我编写了以下代码来更改 MouseOver 上的 MahApps Tile 的背景:

I have written the following code to change the background of a MahApps Tile on MouseOver:

<local:ColorLightConverter x:Key="colorLightConverter" />
    <Style x:Key="aaa" TargetType="mah:Tile">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{Binding Path=Background.Color, RelativeSource={RelativeSource TemplatedParent}, Mode=OneTime, Converter={StaticResource colorLightConverter}}" />
            </Trigger>
        </Style.Triggers>
    </Style>

颜色光转换器与所提到的帖子中所写的相同,并且样式用于 Mahapps Metro Tile.该代码不起作用,因为图块在 MouseOver 上闪烁.另外,在转换器内部放置一个断点,我看到它没有达到.

The color light converter is the same as written in the post mentioned and the style is used on the Mahapps Metro Tile. The code does not work, because the tile flickers on MouseOver. Also, placing a breakpoint inside the converter, I saw that it is not reached.

我做错了什么?

推荐答案

我可以重现 Tile 控件的问题.它从不输入 Converter 代码,因为 TemplatedParent 为空.当我将 RelativeSource 更改为 AncestorType={x:Type StackPanel} 时,就像我的情况一样,闪烁消失了,并且到达了转换器中的断点.您可以通过添加到 Binding FallbackValue=Red 来检查是否属于这种情况,在这种情况下,鼠标悬停的颜色将为红色,表示绑定错误.

I could reproduce the problem with the Tile control. It never enters the Converter code because TemplatedParent is null. When I changed the RelativeSource to AncestorType={x:Type StackPanel} as it was in my case, the flickering went away and the breakpoint in the converter was reached. You can check if this is the case with you by adding to the Binding FallbackValue=Red in which case the color on mouse over will be Red for a faulty binding.

这是我的工作 XAML:

Here is my working XAML:

<Window.Resources>
        <local:ColorLightConverter x:Key="colorLightConverter" />
        <Style x:Key="aaa" TargetType="{x:Type Control}">
            <Setter Property="Background" Value="White"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{Binding Path=Background.Color, RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Converter={StaticResource colorLightConverter},FallbackValue=Red}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel Background="DarkGoldenrod">
        <mah:Tile Title="Hello!" Style="{StaticResource aaa}" 
                    TiltFactor="2"
                    Width="100" Height="100" 
                    Count="1" x:Name="tile">
        </mah:Tile>
        <TextBox Width="100" Height="100" Style="{StaticResource aaa}">Test</TextBox>
    </StackPanel>

这篇关于如何在 WPF 中更改鼠标悬停时的瓷砖背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆