我正在使用MahApps框架,我有样式(请参阅How to change tile background on mouse over in WPF?)以突出显示MouseOver上的图块。
<local:ColorConverter x:Key="colorConverter" />
<Style x:Key="highlightedTile" TargetType="mah:Tile">
<Setter Property="Background" Value="Purple" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding Path=Background, RelativeSource={RelativeSource Self}, Converter={StaticResource colorConverter}, Mode=OneTime, FallbackValue=red}" />
</Trigger>
</Style.Triggers>
</Style>
颜色转换器代码为:
class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] temp = StringToByteArray(value.ToString().Substring(1, 8)); // Remove #
Color color = Color.FromArgb(temp[0], temp[1], temp[2], temp[3]);
System.Drawing.Color darkColor = System.Windows.Forms.ControlPaint.Dark(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B), 0.1f);
return new SolidColorBrush(Color.FromArgb(darkColor.A, darkColor.R, darkColor.G, darkColor.B));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public static byte[] StringToByteArray(string hex)
{
if (hex.Length % 2 == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static int GetHexVal(char hex)
{
int val = (int)hex;
//For uppercase A-F letters:
return val - (val < 58 ? 48 : 55);
//For lowercase a-f letters:
//return val - (val < 58 ? 48 : 87);
//Or the two combined, but a bit slower:
//return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
}
基本上,我希望能够:
我的代码仅在我在样式中设置非突出显示的背景颜色时才有效(在这种情况下,&#34;紫色&#34;)。如果没有在样式中设置此颜色(删除第一行),代码仅适用于图块的默认蓝色背景颜色(如果我在MainWindow.xaml中设置颜色,则转换器甚至不会被触发,我使用断点验证了。最初我使用了这个绑定,但不起作用:
<Setter Property="Background" Value="{Binding Path=Background.Color, RelativeSource={RelativeSource Self}}" />
我做错了什么?或者这是我要求实际可以实现的目标?