Problem
I have an Button
whose command is ToggleMode
, Inside this button is a FontIcon
and a TextBlock
.
In Styles.xaml I set the foreground color of a Button
to white when the Button
is enabled and to a light gray if the button is disabled. This leads to the Textblocks
foreground color correctly changing when the Button
is disabled because the ToggleMode.CanExecute()
returns false.
However the FontIcons
normal color is a light blue, which I set using the Foreground
property.
When the Button
is disabled the color should be changed to a pale blue improving the impression of the button being disabled.
Code example
Boolean converter
<local:BooleanConverter x:Key="CanExecuteToIconColorConverter">
<local:BooleanConverter.True>
<SolidColorBrush Color="{ThemeResource VividSkyBlueColor}" />
</local:BooleanConverter.True>
<local:BooleanConverter.False>
<SolidColorBrush Color="{ThemeResource PaleVividSkyBlueColor}" />
</local:BooleanConverter.False>
</local:BooleanConverter>
Button
<Button Grid.Row="0" Width="150" Padding="10"
Command="{x:Bind ToggleMode, Mode=OneWay}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<FontIcon FontSize="20" Grid.Column="0"
Glyph="{StaticResource mdi_toggle_off}"
FontFamily="{StaticResource MaterialDesignIconsOutlined}"
Foreground="{ThemeResource VividSkyBlueColor}"/>
<TextBlock Grid.Column="1" Margin="10,0,0,0" Text="Toggle"/>
</Grid>
</Button>
What I tried
I tried using the generic BooleanConverter given here: How do I invert BooleanToVisibilityConverter?
Setting a SolidColorBrush
with the light blue color as True Value and the pale blue as the False Value and Binding the Foreground
property of my FontIcon
to the command but this does not work.
After that I tried to subscribe to the CanExecuteChanged
Event of the Command, saving the new value in a private field and binding to that but this does not work as well because at the time of initializing my view this command is still null.
My next guess would be to subscribe to the property changed event of the command when this is fired the command should'nt be null anymore and I should be able to subscribe to the Event but this seems like a lot boiler code especially if I have various commands I need to do this to.
How can I achieve this easier?
Edit1 How I tried to bind the command to theFontIcon
<FontIcon FontSize="20" Grid.Column="0"
Glyph="{StaticResource mdi_toggle_off}"
FontFamily="{StaticResource MaterialDesignIconsOutlined}"
Foreground="{x:Bind ToggleCommand, Mode=OneWay, Converter={StaticResource CanExecuteToIconColorConverter}}"/>
What worked for me
Following the anser by @AndrewKeepCoding I found out that just binding to the IsEnabled property of the button instead of binding to the command itself:
<Button Grid.Row="0" Width="150" Padding="10"
Command="{x:Bind ToggleMode, Mode=OneWay}"
x:Name="ToggleModeButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<FontIcon FontSize="20" Grid.Column="0"
Glyph="{StaticResource mdi_toggle_off}"
FontFamily="{StaticResource MaterialDesignIconsOutlined}"
Foreground="{x:Bind ToggleModeButton.IsEnabled, Mode=OneWay, Converter={StaticResource CanExecuteToIconColorConverter}}"/>
<TextBlock Grid.Column="1" Margin="10,0,0,0" Text="Toggle"/>
</Grid>
</Button>