I'm writing a custom renderer for a submit button in order to provide a uniform style with my Xamarin.Forms application. I'm having trouble setting the foreground (text) color for a disabled button on the Windows (UWP) side of things.
changing the color for an active button is as simple as
Control.Foreground = new SolidColorBrush(Windows.UI.Colors.Green);
but attempting to figure out how to set the foreground color for a disabled button has led me down a rabbit-hole.
I would like to be able to set this without having to use XAML (like this approach) because I plan to extract these renderers later.
The best would be if you could edit the style of your button, or define it somewhere in resources and then apply it to your button, even from code.
There is other way, theoretically simpler and available from code. If you take a look at button's style, you will see the section for disabled visual state:
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
You see the names of brush resources which are used it that state. It should be enough to change them in code to make all disabled buttons look like you want. Though you need to remember that this will also change behavior when user changes theme and your app is suspended.
private void Button_Click(object sender, RoutedEventArgs e)
{
App.Current.Resources["SystemControlBackgroundBaseLowBrush"] = new SolidColorBrush(Colors.Yellow); // background
App.Current.Resources["SystemControlDisabledBaseMediumLowBrush"] = new SolidColorBrush(Colors.Red); // content
App.Current.Resources["SystemControlDisabledTransparentBrush"] = new SolidColorBrush(Colors.Green); // border
}