如何在WPF中更改鼠标悬停平铺背景?

时间:2016-04-17 09:51:12

标签: wpf xaml tile mahapps.metro

使用此帖中提供的代码:

lighten background color on button click per binding with converter

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

<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上闪烁。另外,在转换器内放置一个断点,我看到它没有到达。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我可以通过Tile控件重现问题。它永远不会输入转换器代码,因为TemplatedParent为null。当我将RelativeSource更改为AncestorType={x:Type StackPanel}时,就像我的情况一样,闪烁消失了,转换器中的断点已经到达。您可以通过添加到绑定FallbackValue=Red来检查是否是这种情况,在这种情况下,鼠标悬停的颜色将为红色,以便进行错误绑定。

这是我的工作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>

答案 1 :(得分:-1)

行。所以这是MainWindow.xaml:

                13062   15062   18080   18082                           
ESTABLISHED     49570      15       0       0   
CLOSE_WAIT          0       0     783     493   
SYN_RECV            0       0     109       0   
TIME_WAIT           0      17       0       0  

这是App.xaml

<mah:MetroWindow 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NQR_GUI_WPF"
    xmlns:prop="clr-namespace:NQR_GUI_WPF.Properties"
    xmlns:Custom="http://metro.mahapps.com/winfx/xaml/controls" 
    x:Class="NQR_GUI_WPF.MainWindow"
    xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    mc:Ignorable="d" Height="600" Width="800" ResizeMode="CanResizeWithGrip" WindowStartupLocation="CenterScreen" TitlebarHeight="28"
    GlowBrush="{DynamicResource AccentColorBrush}" ShowIconOnTitleBar="False" Icon="Pictures/icon.ico"
>
<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, Mode=OneTime, RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Converter={StaticResource colorLightConverter}}" />
            </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>

这是颜色转换器代码:

<Application x:Class="NQR_GUI_WPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:NQR_GUI_WPF"
         xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <BitmapImage x:Key="settingsIcon" UriSource="Pictures/settings1_unpressed.png" />
        <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <!-- Accent and AppTheme setting -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

我正在使用MahApps 1.2.4.0。