简体   繁体   中英

How to release a button after click in WPF?

After click a normal button in wpf, there exists some fading animation effect reminding you the button is selected.

按钮图片

How could I manually remove this effect, and do I need to set a mouse in/out trigger to make the button selected/released?

<Window x:Class="smalltest_button.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Margin="50">click</Button>
</Grid>

The effect that you mention is part of the default ControlTemplate of the Button class. One way to remove it is to provide your own custom ControlTemplate . This will enable you make it look any way that you desire. Here is a quick example:

<Button Margin="50">click
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border CornerRadius="4" BorderBrush="Black" BorderThickness="1">
                <Border.Style>
                    <Style TargetType="{x:Type Border}">
                        <Setter Property="Background" Value="LightGreen" />
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="LightBlue" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <TextBlock Text="{TemplateBinding Content}" 
                    HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

Please see the ControlTemplate Class page on MSDN for further help.

Try this by removing the the RenderDefaulted state.

<Button Margin="50" Click="Button_Click">click</Button>

private void Button_Click(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    dynamic chrome = VisualTreeHelper.GetChild(button, 0);
    if (chrome != null && chrome.GetType().Name == "ButtonChrome")
    {
        chrome.RenderDefaulted = false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM