Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:16:44+00:00 2026-05-23T20:16:44+00:00

At the code below, when a mouse is over the grid, the grid’s Background

  • 0

At the code below, when a mouse is over the grid, the grid’s Background is expected to be red, but it isn’t executed as expected.

<Grid>
    <Grid.Style>
    <Style TargetType="{x:Type Grid}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
        </Style>
    </Grid.Style>
</Grid>

However, if I added the Setter to make the Background green, it would be executed properly.

<Grid>
    <Grid.Style>
    <Style TargetType="{x:Type Grid}">
        <Setter Property="Background" Value="Green"/><!-- at the former, added code-->
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
        </Style>
    </Grid.Style>
</Grid>

I don’t know why it is, but guess that there is a precedence for setting the Background, causing the issue. Here is Dependency Property Value Precedence from MSDN, and I understand the precedence of of that reference, but I cannot link this issue to the precedence(MSDN).

Additionally, at above the code snippets, if the Grid is replaced with a Button, both these code will not be executed as expected.


UPDATED: Adding Button case about this issue

<Button>
    <Button.Style >
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background"  Value="Transparent"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:16:45+00:00Added an answer on May 23, 2026 at 8:16 pm

    The problem is your Grid has a null background, so it is not visible for mouse hit testing. If you set the Background to Transparent, then it will be hit-testable like when you set it to green.

    More information can be found here.

    I tried your Button and the Background is initialized to Transparent properly. The Red value is only shown for a fraction of a second. This is because in the Aero theme (I’m on Windows 7) the Button’s ControlTemplate uses a custom chrome to provide animated state transitions (i.e. when hovering etc). This custom chrome element uses internal brushes and it ignores the Background property.

    This has nothing to do with property precedence. For the Grid, it’s simply a matter of your Grid not being hit-testable. So it’s IsMouseOver will not be set to true, unless it has a non-null background (or a child which renders something).

    You can see the Precedence in action here:

    <Grid Background="Blue">
        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                 <Setter Property="Background" Value="Green"/>
                 <Style.Triggers>
                      <Trigger Property="IsMouseOver" Value="True">
                           <Setter Property="Background" Value="Red"/>
                      </Trigger>
                  </Style.Triggers>
            </Style>
        </Grid.Style>
    </Grid>
    

    In the above, the Grid will always be Blue as that has the highest precedence (i.e. local or #3). While Red (#6) takes precedence over Green (#8).

    In the case of the Button, you have something like this:

    <Button Background="Blue">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                 <Setter Property="Template">
                     <Setter.Value>
                         <ControlTemplate TargetType="Button">
                             <Border x:Name="border" Background="{TemplateBinding Background}">
                                 <ContentPresenter />
                             </Border >
                             <ControlTemplate.Triggers>
                                 <Trigger Property="IsMouseOver" Value="True">
                                     <Setter TargetName="border" Property="Background" Value="Red"/>
                                 </Trigger>
                             </ControlTemplate.Triggers>
                         </ControlTemplate>
                     </Setter.Value>
                  </Setter>
            </Style>
        </Button.Style>
    </Button>
    

    In this case, there are two Background properties in play: the Button and the Border in the control template. The Button’s Background property is used by the Border by default, but when the mouse is hovering it uses a Red brush. At that point, it doesn’t matter what the value of the Button’s Background property is set to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some mouse-tracking code that isn't working as expected in IE that basically
The below code doesn't work. I want when the mouse hovers over a div
Code below is not working as expected to detect if it is in design
The code below works. But if I comment out the line Dim objRequest As
The code below gives an error: Property 'Int32 Key' is not defined for type
The code below continues many lines until it ends with a expected /veotherwise /vechoose.
I have this code below which works fine but seems unnecessary as it loops
I'm trying to send a string via ftp using the code below but I'm
I need to register mouse movement for a screensaver like feature. The code below
All, In the code below, <html> <head> <link type=text/css href=http://jqueryui.com/latest/themes/base/ui.all.css rel=stylesheet /> <script type=text/javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.