简体   繁体   中英

WPF XAML - Override a button's MouseOver look

I tried all the other solutions found on this site or on others but it doesn't work. Here is button:

<Button Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" Margin="10,10,5,10" BorderThickness="3">
    <Button.Background>
        <SolidColorBrush Opacity="0.0" Color="#b3b3b3"/>
    </Button.Background>
    <Button.BorderBrush>
        <SolidColorBrush Opacity="0.3" Color="Gray"/>
    </Button.BorderBrush>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="5*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    </Grid>
</Button>

What I tried:

Addig a style in the Windows.Resources :

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="Gray"/>
    </Trigger>
</Style.Triggers>

And adding this inside the button as well. The two solutions I got was either nothing, the old highlighted look, or an error that I can't change the Content twice . As I'm out of ideas, could someone show me how to change my button's highlighted look?

EDIT:

copied character by character from the thread I mentioned in my answer:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Green"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="DarkGoldenrod"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

Now it works. The reason it didn't was that my own button - in this question - had some of its own properties, like those brushes and the grid .

Update: all solutions would work, the reason they didn't for me is that my buttons define their own style inside of them. Once I commented my old buttons out and added a bare button like this:

EDIT:

Here I said define their own style . It was a wrong way to call it, I meant the brushes and the grid by it.

This thread did the trick.

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