Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Converters are an easy way to handle situations where the source and target properties are of a different type. These are often declared in the .Resources
of your app or page and can be referenced in XAML. In some cases, default converter values can be inverted by setting ConverterParameter=True
.
BoolNegationConverter
Converts a boolean to the inverse value (True to False and vice versa)
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.BoolNegationConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:BoolNegationConverter x:Key="BoolNegationConverter" />
</Page.Resources>
<Button Content="Inverted value set as IsEnabled"
IsEnabled="{x:Bind MyBooleanValue, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}" />
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleBoolOption("MyBooleanValue", true, Title = "Original value")]
[ToolkitSample(id: nameof(BoolNegationConverterSample), "BoolNegationConverter", description: $"A sample for showing how to use the BoolNegationConverter.")]
public sealed partial class BoolNegationConverterSample : Page
{
public BoolNegationConverterSample()
{
this.InitializeComponent();
}
}
BoolToObjectConverter
Converts a boolean value into an other object. Can be used to convert true/false to e.g. visibility, color, or different images.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.BoolToObjectConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:BoolToObjectConverter x:Key="BoolToObjectConverter"
FalseValue="{ThemeResource ControlStrongFillColorDefaultBrush}"
TrueValue="{ThemeResource AccentFillColorDefaultBrush}" />
</Page.Resources>
<Border Width="64"
Height="64"
HorizontalAlignment="Left"
Background="{x:Bind MyBooleanValue, Mode=OneWay, Converter={StaticResource BoolToObjectConverter}}"
CornerRadius="4" />
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleBoolOption("MyBooleanValue", false, Title = "Toggle to change colors")]
[ToolkitSample(id: nameof(BoolToObjectConverterSample), "BoolToObjectConverter", description: $"A sample for showing how to use the BoolToObjectConverter.")]
public sealed partial class BoolToObjectConverterSample : Page
{
public BoolToObjectConverterSample()
{
this.InitializeComponent();
}
}
BoolToVisibilityConverter
Converts a boolean value into a Visibility
enumeration. The ConverterParameter
can be used to invert the logic.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.BoolToVisibilityConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="PlaceholderCardStyle"
TargetType="Border">
<Style.Setters>
<Setter Property="Width" Value="64" />
<Setter Property="Height" Value="64" />
<Setter Property="Background" Value="{ThemeResource CardBackgroundFillColorDefaultBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource CardStrokeColorDefaultBrush}" />
<Setter Property="BorderThickness" Value="1" />
</Style.Setters>
</Style>
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Page.Resources>
<StackPanel Orientation="Horizontal"
Spacing="12">
<Border Style="{StaticResource PlaceholderCardStyle}">
<Image HorizontalAlignment="Left"
Source="/Assets/Converters.png"
Visibility="{x:Bind MyBooleanValue, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />
</Border>
<Border Style="{StaticResource PlaceholderCardStyle}">
<Image HorizontalAlignment="Left"
Source="/Assets/Converters.png"
Visibility="{x:Bind MyBooleanValue, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=True}" />
</Border>
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleBoolOption("MyBooleanValue", true, Title = "Toggle to show or hide the image")]
[ToolkitSample(id: nameof(BoolToVisibilityConverterSample), "BoolToVisibilityConverter", description: $"A sample for showing how to use the BoolToVisibilityConverter.")]
public sealed partial class BoolToVisibilityConverterSample : Page
{
public BoolToVisibilityConverterSample()
{
this.InitializeComponent();
}
}
ColorToDisplayNameConverter
Converts a color to the approximated display name.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.ColorToDisplayNameConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d">
<Page.Resources>
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
</Page.Resources>
<StackPanel HorizontalAlignment="Left"
Spacing="16">
<ColorPicker x:Name="colorPicker"
Height="64"
VerticalAlignment="Top"
ColorSpectrumShape="Box"
IsAlphaEnabled="False"
IsAlphaSliderVisible="True"
IsAlphaTextInputVisible="True"
IsColorChannelTextInputVisible="False"
IsColorSliderVisible="False"
IsHexInputVisible="False"
IsMoreButtonVisible="False" />
<TextBlock Text="{Binding ElementName=colorPicker, Path=Color, Converter={StaticResource ColorToDisplayNameConverter}}" />
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSample(id: nameof(ColorToDisplayNameConverterSample), "ColorToDisplayNameConverter", description: $"A sample for showing how to use the ColorToDisplayNameConverter.")]
public sealed partial class ColorToDisplayNameConverterSample : Page
{
public ColorToDisplayNameConverterSample()
{
this.InitializeComponent();
}
}
DoubleToObjectConverter
Converts a double value into an other object. Can be used to convert doubles to e.g. visibility, colors , or different images. If GreaterThan
and LessThan
are both set, the logic looks for a value between the two values. Otherwise the logic looks for the value to be GreaterThan
or LessThan
the specified value.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.DoubleToObjectConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:DoubleToObjectConverter x:Key="DoubleToObjectConverter"
FalseValue="{ThemeResource SystemFillColorSuccessBrush}"
GreaterThan="7"
LessThan="3"
TrueValue="{ThemeResource SystemFillColorCriticalBrush}" />
</Page.Resources>
<Border Width="64"
Height="64"
HorizontalAlignment="Left"
Background="{x:Bind NumericValue, Mode=OneWay, Converter={StaticResource DoubleToObjectConverter}}"
CornerRadius="4" />
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleNumericOption("NumericValue", 0, 0, 10, 1, false, Title = "Number")]
[ToolkitSample(id: nameof(DoubleToObjectConverterSample), "DoubleToObjectConverter", description: $"A sample for showing how to use the DoubleToObjectConverter.")]
public sealed partial class DoubleToObjectConverterSample : Page
{
public DoubleToObjectConverterSample()
{
this.InitializeComponent();
}
}
DoubleToVisibilityConverter
Converts a double value into an other object. Can be used to convert doubles to e.g. visibility, colors , or different images. If GreaterThan
and LessThan
are both set, the logic looks for a value between the two values. Otherwise the logic looks for the value to be GreaterThan
or LessThan
the specified value.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.DoubleToVisibilityConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:DoubleToVisibilityConverter x:Key="DoubleToVisibilityConverter"
GreaterThan="5" />
</Page.Resources>
<Grid Width="64"
Height="64"
HorizontalAlignment="Left"
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="1">
<Image HorizontalAlignment="Left"
Source="/Assets/Converters.png"
Visibility="{x:Bind NumericValue, Mode=OneWay, Converter={StaticResource DoubleToVisibilityConverter}, ConverterParameter=True}" />
</Grid>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleNumericOption("NumericValue", 0, 0, 10, 1, false, Title = "Number")]
[ToolkitSample(id: nameof(DoubleToVisibilityConverterSample), "DoubleToVisibilityConverter", description: $"A sample for showing how to use the DoubleToObjectConverter.")]
public sealed partial class DoubleToVisibilityConverterSample : Page
{
public DoubleToVisibilityConverterSample()
{
this.InitializeComponent();
}
}
EmptyCollectionToObjectConverter & CollectionVisibilityConverter
Converts a collection size into an other object. Can be used to convert to bind a visibility, a color or an image to the size of the collection. The CollectionVisibilityConverter
is derived from it and can be easily used to convert a collection into a Visibility
enumeration (Collapsed if the given collection is empty or null).
Note: this converter only checks the initial state of a collection and does not detect any updates. For that scenario, using a IsNullOrEmptyStateTrigger
is more appropriate.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.CollectionVisibilityConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:CollectionVisibilityConverter x:Key="CollectionVisibilityConverter" />
</Page.Resources>
<Grid>
<TextBlock HorizontalAlignment="Center"
TextAlignment="Center"
Visibility="{x:Bind EmptyCollection, Mode=OneWay, Converter={StaticResource CollectionVisibilityConverter}, ConverterParameter=True}">
<Run FontWeight="SemiBold"
Text="All done for the day" />
<LineBreak />
<Run Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="Enjoy your empty inbox" />
</TextBlock>
</Grid>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSample(id: nameof(CollectionVisibilityConverterSample), "CollectionVisibilityConverter", description: $"A sample for showing how to use the CollectionVisibilityConverter.")]
public sealed partial class CollectionVisibilityConverterSample : Page
{
public ObservableCollection<string> EmptyCollection = new();
public CollectionVisibilityConverterSample()
{
this.InitializeComponent();
}
}
EmptyObjectToObjectConverter, EmptyStringToObjectConverter & StringToVisibilityConverter
Converts an object value into a an object (if the value is null returns the false value). Can be used to bind a visibility, a color or an image to the value of an object.
The EmptyStringToObjectConverter
converts a string value into a an object (if the value is null or empty returns the false value). Can be used to bind a visibility, a color or an image to the value of a string.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.EmptyStringToObjectConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:EmptyStringToObjectConverter x:Key="EmptyStringToObjectConverter"
EmptyValue="False"
NotEmptyValue="True" />
</Page.Resources>
<StackPanel>
<Button Content="Submit"
IsEnabled="{x:Bind MyTextStringValue, Mode=OneWay, Converter={StaticResource EmptyStringToObjectConverter}}"
Style="{StaticResource AccentButtonStyle}" />
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleTextOption("MyTextStringValue", "", Title = "Enter text to enable the button")]
[ToolkitSample(id: nameof(EmptyStringToObjectConverterSample), "EmptyStringToObjectConverter", description: $"A sample for showing how to use the EmptyStringToObjectConverter.")]
public sealed partial class EmptyStringToObjectConverterSample : Page
{
public EmptyStringToObjectConverterSample()
{
this.InitializeComponent();
}
}
FileSizeToFriendlyStringConverter
Converts a file size in bytes to a more human-readable friendly format using ToFileSizeString(Int64)
.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.FileSizeToFriendlyStringConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:FileSizeToFriendlyStringConverter x:Key="FileSizeToFriendlyStringConverter" />
</Page.Resources>
<StackPanel Orientation="Vertical"
Spacing="12">
<TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}">
<Run Text="Original value (Bytes):" />
<Run FontWeight="SemiBold"
Text="{x:Bind FileSizeInBytes}" />
</TextBlock>
<TextBlock>
<Run Text="Converted string:" />
<Run FontWeight="SemiBold"
Text="{x:Bind FileSizeInBytes, Converter={StaticResource FileSizeToFriendlyStringConverter}}" />
</TextBlock>
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSample(id: nameof(FileSizeToFriendlyStringConverterSample), "FileSizeToFriendlyStringConverter", description: $"A sample for showing how to use the FileSizeToFriendlyStringConverter.")]
public sealed partial class FileSizeToFriendlyStringConverterSample : Page
{
public long FileSizeInBytes = 2400000;
public FileSizeToFriendlyStringConverterSample()
{
this.InitializeComponent();
}
}
ResourceNameToResourceStringConverter
Converts a source string from the App resources and returns its value, if found.
StringFormatConverter
This allows you to format a string property upon binding wrapping string.Format.
It only allows for a single input value (the binding string), but can be formatted with the regular string.Format
methods.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.StringFormatConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:StringFormatConverter x:Key="StringFormatConverter" />
</Page.Resources>
<StackPanel Spacing="16">
<TextBlock Text="{x:Bind MyBooleanValue, Mode=OneWay, Converter={StaticResource StringFormatConverter}, ConverterParameter='The page is loading: {0}'}" />
<TextBlock Text="{x:Bind MyDoubleValue, Mode=OneWay, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}There are {0:0.##} pixels'}" />
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.WinUI.Converters;
namespace ConvertersExperiment.Samples;
/// <summary>
/// An example sample page of a custom control inheriting from Panel.
/// </summary>
[ToolkitSampleBoolOption("MyBooleanValue", true, Title = "Is loading")]
[ToolkitSampleNumericOption("MyDoubleValue", 1, 0, 10, 0.25, false, Title = "Number of pixels")]
[ToolkitSample(id: nameof(StringFormatConverterSample), "StringFormatConverter", description: $"A sample for showing how to use the StringFormatConverter.")]
public sealed partial class StringFormatConverterSample : Page
{
public StringFormatConverterSample()
{
this.InitializeComponent();
}
}
StringVisibilityConverter
Converts a string value into a Visibility value (if the value is null or empty returns a collapsed value).
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.StringVisibilityConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:StringVisibilityConverter x:Key="StringVisibilityConverter" />
</Page.Resources>
<TextBlock Text="This text is visible when the input TextBox is empty"
Visibility="{x:Bind MyTextStringValue, Mode=OneWay, Converter={StaticResource StringVisibilityConverter}, ConverterParameter=True}" />
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleTextOption("MyTextStringValue", "", Title = "Enter text to hide the TextBlock")]
[ToolkitSample(id: nameof(StringVisibilityConverterSample), "StringVisibilityConverter", description: $"A sample for showing how to use the StringVisibilityConverter.")]
public sealed partial class StringVisibilityConverterSample : Page
{
public StringVisibilityConverterSample()
{
this.InitializeComponent();
}
}
TaskResultConverter
Converter that can be used to safely retrieve results from Task<TResult>
instances. See the MVVM Toolkit Gallery for an example.
TypeToObjectConverter
Returns an object or another, depending on whether the type of the provided value matches another provided Type.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.TypeToObjectConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<converters:TypeToObjectConverter x:Key="TypeToObjectConverter"
FalseValue="Red"
TrueValue="Green"
Type="x:String" />
</Page.Resources>
<StackPanel Spacing="8">
<ComboBox x:Name="Selection"
SelectedIndex="0">
<ComboBoxItem>
<x:Int32>64</x:Int32>
</ComboBoxItem>
<ComboBoxItem>
<x:Double>44.44</x:Double>
</ComboBoxItem>
<ComboBoxItem>I'm a String!</ComboBoxItem>
</ComboBox>
<TextBlock Foreground="{x:Bind ((ComboBoxItem)Selection.SelectedItem).Content, Mode=OneWay, Converter={StaticResource TypeToObjectConverter}}"
Text="This text is Green when a string is selected" />
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSample(id: nameof(TypeToObjectConverterSample), "TypeToObjectConverter", description: $"A sample for showing how to use the TypeToObjectConverter.")]
public sealed partial class TypeToObjectConverterSample : Page
{
public TypeToObjectConverterSample()
{
this.InitializeComponent();
}
}
VisibilityToBoolConverter
Converts a Visibility
enumeration to a boolean value.
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="ConvertersExperiment.Samples.VisibilityToBoolConverterSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:ConvertersExperiment.Samples"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="PlaceholderCardStyle"
TargetType="Border">
<Style.Setters>
<Setter Property="Width" Value="64" />
<Setter Property="Height" Value="64" />
<Setter Property="Background" Value="{ThemeResource CardBackgroundFillColorDefaultBrush}" />
<Setter Property="BorderBrush" Value="{ThemeResource CardStrokeColorDefaultBrush}" />
<Setter Property="BorderThickness" Value="1" />
</Style.Setters>
</Style>
<converters:VisibilityToBoolConverter x:Key="VisibilityToBoolConverter" />
</Page.Resources>
<StackPanel Spacing="12">
<Border Style="{StaticResource PlaceholderCardStyle}">
<Image x:Name="image"
HorizontalAlignment="Left"
Source="/Assets/Converters.png"
Visibility="{x:Bind MyBooleanValue, Mode=OneWay}" />
</Border>
<TextBlock>
<Run Text="Image visibility:" />
<Run FontWeight="SemiBold"
Text="{Binding ElementName=image, Path=Visibility, Mode=OneWay, Converter={StaticResource VisibilityToBoolConverter}}" />
</TextBlock>
</StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace ConvertersExperiment.Samples;
[ToolkitSampleBoolOption("MyBooleanValue", true, Title = "Toggle to show or hide the image")]
[ToolkitSample(id: nameof(VisibilityToBoolConverterSample), "VisibilityToBoolConverter", description: $"A sample for showing how to use the VisibilityToBoolConverter.")]
public sealed partial class VisibilityToBoolConverterSample : Page
{
public VisibilityToBoolConverterSample()
{
this.InitializeComponent();
}
}
Windows Community Toolkit