简体   繁体   中英

Override IsMouseOver and change background color based on logic in WPF Button

Goal To make the default behavior for a buttons background onMouseEnter be to do nothing, yet still allow for programatically changing the button background in the logic of the application.

Multiple posts address overriding the default MouseOver behaviour:

ex: How do you disable MouseOver effects on a Button in WPF?

However, in my application I need to keep changing the background color, and setting the MouseOver background color as a Trigger overrides my changes.

 <Button Content="Start" Name="buttonStart" Style="{StaticResource mainButton}" PreviewMouseDown="buttonStart_MouseDown"/>
 <Button Content="Stop" Name="buttonStop" Style="{StaticResource mainButton}" PreviewMouseDown="buttonStop_MouseDown"/>

Style in App.xaml

<Style x:Key="mainButton" TargetType="{x:Type Button}">

           <Setter Property="Width" Value="250"/>
            <Setter Property="Height" Value="75"/>
            <Setter Property="FontSize" Value="40"/>
            <Setter Property="FontFamily" Value="Segoe UI Light"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="Border"  BorderThickness="1"

                BorderBrush="#404040">
                        <ContentPresenter Margin="2" 
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center" 
                             RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                     <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Border" 
                      Property="Background" Value="{x:Null}" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>


        </Style>

Logic:

private void buttonStop_MouseDown(object sender, RoutedEventArgs e)
        {

        //Change background colors for buttonStop and buttonStart

    }

private void buttonStart_MouseDown(object sender, RoutedEventArgs e)
        {

        //Change background colors for buttonStop and buttonStart

    }

How do I make the background on buttons do nothing onMouseEnter, yet still change the background in the code?

If you cannot remove the Trigger you could use a TextBlock for the content on your button then set the background of the TextBlock OnClick in your code behind. You would have to adjust the style to allow for the TextBlock to fill the Button for it to appear correct.

Something like this:

private void buttonStop_MouseDown(object sender, RoutedEventArgs e)
{
    //Change background colors for buttonStop and buttonStart
    TextBlock tb = new TextBlock();
    tb.Background = Brushes.Blue;
    ((Button)sender).Content = tb;
}

......

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