Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the Effect of Button once it is clicked WPF

Tags:

c#

wpf

xaml

I want to apply the following changes when the Button is pressed.

<Button>
    <Button.Effect>
        <DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
    </Button.Effect>
</Button>
like image 573
Bikram Limbu Avatar asked Sep 18 '17 12:09

Bikram Limbu


1 Answers

What about handling the Click event for the Button?

<Button Content="Click me!" Click="Button_Click_1" Margin="10" />

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    button.Effect = new System.Windows.Media.Effects.DropShadowEffect()
    {
        BlurRadius = 10,
        ShadowDepth = 5
    };
}

Or use a Style that binds to the IsPressed property if you want to display the shadow only while the Button is being pressed:

<Button Content="Button">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Or use a ToggleButton and set the Effect property based on its IsChecked property:

<ToggleButton>
    <ToggleButton.Style>
        <Style TargetType="ToggleButton">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Effect">
                        <Setter.Value>
                            <DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>
like image 71
mm8 Avatar answered Oct 15 '22 15:10

mm8