In WPF one could use System.Windows.Data.Binding.DoNothing
to create a converter that falls back to default styling, for example.
Which solution for WinUI3?
In particular I’m try to code a Converter that override a text color only if required. If conditions are not met, the text color should be using default styling.
Here’s a sample code that should achieve what you are trying to implement.
<Page
x:Class="WinUIApp5.MainPage"
xmlns:toolkit="using:CommunityToolkit.WinUI.Converters"
...>
<Page.Resources>
<SolidColorBrush
x:Key="BlueBrush"
Color="Blue" />
<toolkit:BoolToObjectConverter
x:Key="TrueToBlueConverter"
FalseValue="{StaticResource TextControlForeground}"
TrueValue="{StaticResource BlueBrush}" />
</Page.Resources>
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ToggleSwitch x:Name="BlueForegroundToggleSwitch" />
<TextBox
Foreground="{x:Bind BlueForegroundToggleSwitch.IsOn, Mode=OneWay, Converter={StaticResource TrueToBlueConverter}}"
Text="ABCDE" />
</StackPanel>
</Page>
The BoolToObjectConverter comes from the CommunityToolkit.WinUI.Converters NuGet package. If you want to implement the converter yourself, you can find the code here.
The TextControlForeground
is the resource used in the DefaultTextBoxStyle
. You can learn about the default styles in the generic.xaml file.