0

I would like to change the background color of the button to red, when hovering over it and when not hovering over it, it should be white, I tried using got focus and lost focus but they dont work. Please help me Thanks in advance!

  • 1
    This question should not have been marked as already answered with a link to a WPF solution. The question was how to do this in UWP, *not* WPF, and the implementation is completely different between the two platforms. – Tam Bui Mar 03 '23 at 20:34

1 Answers1

1

You can change the background and foreground when hovering over the button in the figure below. You can specify the colors depending on the Dark and Light theme usage. This makes sense if it will be applied for only one button. You will need to override this in App.xaml if all buttons will be of the same design.

You can find more detailed information here.

<Button Background="Red" Foreground="Black" Content="BUTTON">
        <Button.Resources>
            <ResourceDictionary>
                <ResourceDictionary.ThemeDictionaries>
                    <ResourceDictionary x:Key="Dark">
                        <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="Blue"/>
                        <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="White"/>
                    </ResourceDictionary>
                    <ResourceDictionary x:Key="Light">
                        <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="Blue"/>
                        <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="White"/>
                    </ResourceDictionary>
                </ResourceDictionary.ThemeDictionaries>
            </ResourceDictionary>
        </Button.Resources>
    </Button>
Ozgur Saklanmaz
  • 528
  • 3
  • 17
  • Microsoft's documentation on changing button interactions is limited to showing only this light-weight styling solution. Keep in mind that if you write your own Button ControlTemplate (which is very common for UX designers to re-template a button), this answer no longer works. You have to resort to writing a VisualStateManager in XAML to control all of the button states, which is much more verbose. – Tam Bui Mar 03 '23 at 20:39