Dark color

I want to change the background color of an element when the mouseEnter event fires. How to make the background color darker? I thought I could use an opacity mask, but this is a gradient, but I need it to be solid. It should also be in visual base code, not in xaml. Please help me!

+3
source share
2 answers

The opacity mask is not very good, as it changes the opacity. In addition, opacitymask can be any brush, it should not be a gradient.

You can do one of two things: manipulate the current brush or add a black rectangle over the control and change the opacity of the rectangle.

If you let me know what you prefer, I can write code.

( , xaml?)

<Window x:Class="TestWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWpfApplication"
        Title="MainWindow"
        Height="350"
        Width="525">

    <StackPanel>
        <Grid>
            <TextBox Background="Red"
                     FontSize="24" />
            <Rectangle x:Name="overlay"
                       Fill="Black"
                       IsHitTestVisible="False"
                       Opacity="0" />
            <Grid.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation To="0.9"
                                             Duration="0:0:0.2"
                                             Storyboard.TargetName="overlay"
                                             Storyboard.TargetProperty="(Rectangle.Opacity)" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation To="0"
                                             Duration="0:0:0.2"
                                             Storyboard.TargetName="overlay"
                                             Storyboard.TargetProperty="(Rectangle.Opacity)" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Grid.Triggers>
        </Grid>
    </StackPanel>
</Window>
+2

ValueConverter. :

public class ChangeColorOpacityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Color input = (Color)value;
        input.A = byte.Parse((string)parameter); //Changes alpha to ValueConverterParameter
        return input;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

, , , .


VC:

<Border>
    <Border.Resources>
        <vc:DarkenColorConverter x:Key="DarkenColorConverter"/>
    </Border.Resources>
    <Border.Background>
        <SolidColorBrush Color="{Binding MyColor, Converter={StaticResource DarkenColorConverter}}"/>
    </Border.Background>
</Border>

, ConverterParameter.

+1

All Articles