您可以创建一个IValueConverter
使颜色更深
像这样的东西应该可以解决问题
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double percentage = 0.8; // Default
if (value is SolidColorBrush)
{
if (parameter != null)
{
double.TryParse(parameter.ToString(), out percentage);
}
Color color = (value as SolidColorBrush).Color;
return new SolidColorBrush(Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage)));
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
这是一个使用示例
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication13"
Title="MainWindow" x:Name="UI" Width="124" Height="336"
>
<Window.Resources>
<!-- Converter -->
<local:DarkenColorConverter x:Key="Darken" />
<!-- Brush to manipulate -->
<SolidColorBrush x:Key="red" Color="{Binding Source=Red}" />
</Window.Resources>
<StackPanel>
<!-- Original color -->
<Rectangle Fill="{StaticResource red}" Width="100" Height="100" />
<!-- Darken with Converter -->
<Rectangle Fill="{Binding Source={StaticResource red}, Converter={StaticResource Darken}}" Width="100" Height="100"/>
<!-- Using ConverterParameter to select how dark (0.0 - 1.0) -->
<Rectangle Fill="{Binding Source={StaticResource red}, Converter={StaticResource Darken}, ConverterParameter=0.5}" Width="100" Height="100"/>
</StackPanel>
</Window>
结果: