Search code examples
c#windowsxamluwp

Change button color on hover UWP


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!


Solution

  • 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>