I'm working on a UWP project, and I need to change the button's background and foreground colors as the app is running. I have managed to do that, but I also have a piece of code that let me disable the annoying hover effect present in all UWP buttons.
There is my App.xaml code:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="White"/>
<SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Gray"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
I was wondering how can I change the "color" property to match each button's state (they have different colors and backgrounds). Is there an option to set the color to the ButtonBackground property by default. If not, how can I change each button's style at runtime?
CodePudding user response:
You have many ways to change button's style in the runtime, as you code mentioned above, you have place the ButtonForegroundPointerOver
SolidColorBrush in the ThemeDictionaries
and give them light dark or default key, when you change the app's theme, it will apply the matched ButtonForegroundPointerOver for button control that used the default style.
how can I change each button's style at runtime?
Button's style property could be set at runtime, you could make different button style with key name, and load it from page or application resource then apply to button style.
For more info please refer XAML styles document.