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

  • SEARCH
  • Home
  • 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 9234667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:50:38+00:00 2026-06-18T06:50:38+00:00

I am creating a WPF button using Windows 8 styling (formerly metro). I would

  • 0

I am creating a WPF button using Windows 8 styling (formerly metro).

I would like the focused state of the button to show with a solid background. When the mouse is over the control, I would like th background to darken slightly to create the visual cue that the button can be clicked.

Unfortunately, the XAML I’ve written below does not work. The focused state shows correctly, but when the mouse is over the control, the background does not darken as I would like it to.

<Color x:Key="DoxCycleGreen">
    #FF8DC63F
</Color>

<!-- Soft Interface : DoxCycle Green --> 
<Color x:Key="DoxCycleGreenSoft">
    #FFC0DC8F
</Color>

<Style x:Key="MetroButton" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="RootElement">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal" />
                            <VisualState Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BackgroundColor" Storyboard.TargetProperty="Color" To="{StaticResource DoxCycleGreen}" Duration="0:0:0.150" />
                                    <ColorAnimation Storyboard.TargetName="FontColor" Storyboard.TargetProperty="Color" To="White" Duration="0:0:0.150" />
                                </Storyboard>
                            </VisualState>
                            <VisualState Name="Focused">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BackgroundColor" Storyboard.TargetProperty="Color" To="{StaticResource DoxCycleGreenSoft}" Duration="0:0:0.150" />
                                    <ColorAnimation Storyboard.TargetName="FontColor" Storyboard.TargetProperty="Color" To="White" Duration="0:0:0.150" />
                                </Storyboard>
                            </VisualState>
                            <VisualState Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BackgroundColor" Storyboard.TargetProperty="Color" To="Transparent" Duration="0:0:0.150" />
                                    <ColorAnimation Storyboard.TargetName="FontColor" Storyboard.TargetProperty="Color" To="{StaticResource DoxCycleGreen}" Duration="0:0:0.150" />
                                </Storyboard>
                            </VisualState>
                            <VisualState Name="Disabled">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderColor" Storyboard.TargetProperty="Color" To="DarkGray" Duration="0:0:0.1" />
                                    <ColorAnimation Storyboard.TargetName="FontColor" Storyboard.TargetProperty="Color" To="DarkGray" Duration="0:0:0.1" />                                        
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>


                    <Grid Background="Transparent" >
                        <Border BorderThickness="1,1,1,1" Padding="2">
                            <Border.BorderBrush>
                                <SolidColorBrush x:Name="BorderColor" Color="{StaticResource DoxCycleGreen}"/>
                            </Border.BorderBrush>
                            <Border.Background>
                                <SolidColorBrush x:Name="BackgroundColor" Color="White"/>
                            </Border.Background>

                            <ContentPresenter 
                                              x:Name="ContentSite" 
                                              VerticalAlignment="Center" 
                                              HorizontalAlignment="Center" 
                                              ContentSource="Content">
                                <TextBlock.Foreground>
                                    <SolidColorBrush x:Name="FontColor" Color="{StaticResource DoxCycleGreen}"/>
                                </TextBlock.Foreground>
                            </ContentPresenter>
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
  • 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-06-18T06:50:39+00:00Added an answer on June 18, 2026 at 6:50 am

    I’ve now tested your code. You’ve got a couple of issues at play here. But the main issue is that a WPF control can only be in one Visual State of a particular State Group at a time. And in cases like you’ve got here, where the control can be both focused and moused-over, WPF has to make choice of which State to apply (it cannot apply both States since they are in the same State Group). So in this case it is just keeping it in the Focused state and not sending it to the MouseOver state.

    A control can be in multiple states if each of those states are in different state groups. From this documentation:

    Each VisualStateGroup contains a collection of VisualState objects that are mutually exclusive. That is, the control is always in exactly one state of in each VisualStateGroup.

    So our first step to correcting this code is to include the proper state groups that will allow the button to be able to show its Focused state and then show its MouseOver state (other possibilities can be corrected by this change but that’s the one you noticed in particular that you didn’t get with your previous approach).

    To do this, we need to be careful to name our state groups and (especially) our state names correctly. This is because the code internal to the Button class likely makes a call like VisualStateManager.GoToState(this, "VerySpecificStateName", true); (I have not inspected the actual source code of the Button class to verify this, but having written custom controls where I’ve needed to initiate the state changes, I know it must be something like that). In order to get a list of the state group and state names that we’ll need, we could either use Expression Blend to “edit a copy” of the control template (which will populate the needed states for us), or find them here. That documentation shows us that we need a state group called “FocusStates” and two states in that group called “Focused” and “Unfocused” (along with other state groups and states). As an aside, to illustrate how the Button class is initiating its state changes by these specific named states, if you change your original code by replacing the “Focus” state name with “MisspelledFocus”, you’ll see that your button never enters that state.

    Implementing this first change, we could end up with something like:

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal" />
            <VisualState x:Name="MouseOver">
                <Storyboard>
                    <ColorAnimation Storyboard.TargetName="BackgroundColor" 
                                                    Storyboard.TargetProperty="Color" 
                                                    To="{StaticResource DoxCycleGreen}" Duration="0:0:0.150" />
                    <ColorAnimation Storyboard.TargetName="FontColor"                                           Storyboard.TargetProperty="Color" 
                                                    To="White" Duration="0:0:0.150" />
                </Storyboard>
            </VisualState>
            <VisualState x:Name="Pressed">
                <Storyboard>
                    <ColorAnimation Storyboard.TargetName="BackgroundColor" 
                                                    Storyboard.TargetProperty="Color" 
                                                    To="Transparent" Duration="0:0:0.150" />
                    <ColorAnimation Storyboard.TargetName="FontColor" 
                                                    Storyboard.TargetProperty="Color" 
                                                    To="{StaticResource DoxCycleGreen}" Duration="0:0:0.150" />
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                  <Storyboard>
                    <ColorAnimation Storyboard.TargetName="BorderColor" 
                                                    Storyboard.TargetProperty="Color" To="DarkGray" Duration="0:0:0.1" />
                    <ColorAnimation Storyboard.TargetName="FontColor" 
                                                    Storyboard.TargetProperty="Color" To="DarkGray" Duration="0:0:0.1" />                                        
                  </Storyboard>
              </VisualState>
          </VisualStateGroup>
      <!-- Focus States -->
          <VisualStateGroup x:Name="FocusStates">
                <VisualState x:Name="Focused">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetName="BackgroundColor" 
                                                    Storyboard.TargetProperty="Color" 
                                                    To="{StaticResource DoxCycleGreenSoft}" Duration="0:0:0.150" />
                        <ColorAnimation Storyboard.TargetName="FontColor" 
                                                    Storyboard.TargetProperty="Color" 
                                                    To="White" Duration="0:0:0.150" />
                      </Storyboard>
                </VisualState>
                <VisualState x:Name="Unfocused"/>
           </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
    

    This somewhat solves the problem. However, if you’re looking at this in Expression Blend, you’ll notice a warning in the state group headings:

    Expression Blend warning about changing the same object-property in more than one state group

    We are getting this warning because we are changing the value of an identical property/object pair in more than one state group – in this case the “Color” property of the object named “BackgroundColor”. Why could this be an issue? Because of what I said earlier – the fact that a control can be in multiple states at once if those states are in different state groups. So if the user has given the button focus and the user has also moused-over the button, it could be ambiguous to WPF as to which animation to apply, since both states say to animate the same exact property but in different ways.

    Also, this first change does not completely get us what we want. If you try giving the button focus, then hover over it, it correctly goes from “Normal”, to “Focused”, to “MouseOver”. But if you now discontinue hovering, you’ll see that the button does not return to its “Focused” state.

    There are several approaches you could use to remedy this problem and achieve something similar to what you wanted, but just as an example, we could do something like this. (This may not be the cleanest implementation for this, but it fixes the common object/property issue.):

    <Color x:Key="DoxCycleGreen">
        #FF8DC63F
    </Color>
    
    <SolidColorBrush x:Key="DoxCycleGreenBrush" Color="{StaticResource DoxCycleGreen}" />
    
    <!-- Soft Interface : DoxCycle Green --> 
    <Color x:Key="DoxCycleGreenSoft">
        #FFC0DC8F
    </Color>
    
    <SolidColorBrush x:Key="DoxCycleGreenSoftBrush" Color="{StaticResource DoxCycleGreenSoft}" />
    
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="RootElement">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="BackgroundColor" 
                                                        Storyboard.TargetProperty="Color" 
                                                        To="{StaticResource DoxCycleGreen}" Duration="0:0:0.150" />
                                        <ColorAnimation Storyboard.TargetName="FontColor" 
                                                        Storyboard.TargetProperty="Color" 
                                                        To="White" Duration="0:0:0.150" />
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="MouseOverBorder">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="BackgroundColor" 
                                                        Storyboard.TargetProperty="Color" 
                                                        To="Transparent" Duration="0:0:0.150" />
                                        <ColorAnimation Storyboard.TargetName="FontColor" 
                                                        Storyboard.TargetProperty="Color" 
                                                        To="{StaticResource DoxCycleGreen}" Duration="0:0:0.150" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="BorderColor" 
                                                        Storyboard.TargetProperty="Color" To="DarkGray" Duration="0:0:0.1" />
                                        <ColorAnimation Storyboard.TargetName="FontColor" 
                                                        Storyboard.TargetProperty="Color" To="DarkGray" Duration="0:0:0.1" />                                        
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <!-- Focus States -->
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.15"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Focused">
                                    <Storyboard>
    
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" 
                                                                       Storyboard.TargetName="FocusBorder">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ContentSiteWhiteForeground">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
    
    
                        <Grid Background="Transparent" >
                            <Border x:Name="BaseBorder" BorderThickness="1,1,1,1">
                                <Border.BorderBrush>
                                    <SolidColorBrush x:Name="BorderColor" Color="{StaticResource DoxCycleGreen}"/>
                                </Border.BorderBrush>
                                <Border.Background>
                                    <SolidColorBrush x:Name="BackgroundColor" Color="White"/>
                                </Border.Background>
                            </Border>
                            <Border x:Name="FocusBorder" 
                                    BorderThickness="1,1,1,1" 
                                    Background="{DynamicResource DoxCycleGreenSoftBrush}" 
                                    Opacity="0" />
    
                            <Border x:Name="MouseOverBorder" 
                                    BorderThickness="1,1,1,1"
                                    Background="{DynamicResource DoxCycleGreenBrush}" 
                                    Opacity="0" />
    
                            <ContentPresenter 
                                x:Name="ContentSite" 
                                VerticalAlignment="Center" 
                                HorizontalAlignment="Center" 
                                ContentSource="Content" Margin="2">
                                <TextBlock.Foreground>
                                    <SolidColorBrush x:Name="FontColor" Color="{StaticResource DoxCycleGreen}"/>
                                </TextBlock.Foreground>
                            </ContentPresenter>
    
                            <ContentPresenter 
                                x:Name="ContentSiteWhiteForeground"
    
                                VerticalAlignment="Center" 
                                HorizontalAlignment="Center" 
                                ContentSource="Content" Margin="2" Opacity="0">
                                <TextBlock.Foreground>
                                    <SolidColorBrush Color="White" />
                                </TextBlock.Foreground>
                            </ContentPresenter>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    Now you’ll see that we have removed the ambiguity for WPF. And we see that it now handles the case of state changes from “Normal” to “Focus” to “MouseOver” and back to “Focus” correctly.

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

Sidebar

Related Questions

I'm creating several WPF windows like this: flightWindow = new FlightWindow(txbNextFlight.Text, airline); flightWindow.Show(); And
I am using MVVM Light toolkit in my WPF application. I would like to
I am creating paint like application using WPF. Now i need to create control
Using the MVVM pattern creating WPF applications you have the ViewModel providing data to
I am creating a WPF app using MVVM. The app manages tagged documents, called
I'm creating a WPF app using the MVVM design pattern, and I'm trying to
I'm creating a basic database application in WPF, and I have started using the
I am creating a WPF custom control, a Button with an Image and Text
I have succeed creating a popup using this code in c# and wpf <Popup
I am creating a page-based WPF application using MVVM. I have created a custom

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.