如何在 WPF 中更改鼠标悬停时的平铺背景?
How to change tile background on mouse over in WPF?
使用此 post 中提供的代码:
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>
彩灯转换器与post中所写的相同,样式用于Mahapps Metro Tile。该代码不起作用,因为磁贴在 MouseOver 上闪烁。另外,在转换器内放置一个断点,我看到它没有到达。
我做错了什么?
我可以重现 Tile 控件的问题。它从不进入转换器代码,因为 TemplatedParent 为空。当我按照我的情况将 RelativeSource 更改为 AncestorType={x:Type StackPanel}
时,闪烁消失并且到达了转换器中的断点。您可以通过添加到 Binding 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>
好的。所以这是 MainWindow.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>
这是App.xaml
<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>
这是颜色转换器代码:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color color = (Color)value;
System.Drawing.Color darkColor = System.Windows.Forms.ControlPaint.Dark(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B), 0.1f);
return System.Drawing.Color.FromArgb(darkColor.A, darkColor.R, darkColor.G, darkColor.B);
}
我正在使用 MahApps 1.2.4.0。
使用此 post 中提供的代码:
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>
彩灯转换器与post中所写的相同,样式用于Mahapps Metro Tile。该代码不起作用,因为磁贴在 MouseOver 上闪烁。另外,在转换器内放置一个断点,我看到它没有到达。
我做错了什么?
我可以重现 Tile 控件的问题。它从不进入转换器代码,因为 TemplatedParent 为空。当我按照我的情况将 RelativeSource 更改为 AncestorType={x:Type StackPanel}
时,闪烁消失并且到达了转换器中的断点。您可以通过添加到 Binding 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>
好的。所以这是 MainWindow.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>
这是App.xaml
<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>
这是颜色转换器代码:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color color = (Color)value;
System.Drawing.Color darkColor = System.Windows.Forms.ControlPaint.Dark(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B), 0.1f);
return System.Drawing.Color.FromArgb(darkColor.A, darkColor.R, darkColor.G, darkColor.B);
}
我正在使用 MahApps 1.2.4.0。