Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
I convertitori sono un modo semplice per gestire le situazioni in cui le proprietà di origine e destinazione sono di un tipo diverso. Questi vengono spesso dichiarati nel .Resources dell'app o della pagina e possono essere referenziati in XAML. In alcuni casi, i valori del convertitore predefiniti possono essere invertiti impostando ConverterParameter=True.
BoolNegationConverter
Converte un valore booleano nel valore inverso (True in False e viceversa)
<!-- 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
Converte un valore booleano in un altro oggetto . Può essere utilizzato per convertire true/false, ad esempio, in valori di visibilità, colori o immagini diverse.
<!-- 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
Converte un valore booleano in un'enumerazione Visibility . Può ConverterParameter essere usato per invertire la logica.
<!-- 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
Converte un colore nel nome visualizzato approssimativo.
<!-- 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
Converte un valore double in un altro oggetto . Può essere usato per convertire i valori double in ad esempio visibilità, colori o immagini diverse. Se GreaterThan e LessThan sono entrambi impostati, la logica cerca un valore tra i due valori. In caso contrario, la logica cerca che il valore sia GreaterThan o LessThan il valore specificato.
<!-- 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="3"
LessThan="7"
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
Converte un valore double in un altro oggetto . Può essere usato per convertire i valori double in ad esempio visibilità, colori o immagini diverse. Se GreaterThan e LessThan sono entrambi impostati, la logica cerca un valore tra i due valori. In caso contrario, la logica cerca che il valore sia GreaterThan o LessThan il valore specificato.
<!-- 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
Converte la dimensione di una raccolta in un altro oggetto. Può essere usato come convertitore per associare la visibilità, un colore o un'immagine alla dimensione della raccolta.
CollectionVisibilityConverter deriva da esso e può essere facilmente utilizzato per convertire una raccolta in un valore di enumerazione Visibility (Collapsed se la raccolta specificata è vuota o null).
Nota: questo convertitore controlla solo lo stato iniziale di una raccolta e non rileva aggiornamenti. Per quello scenario, l'uso di IsNullOrEmptyStateTrigger è più appropriato.
<!-- 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
Converte un valore dell'oggetto in un oggetto (se il valore è Null restituisce il valore false). Può essere usato per associare una visibilità, un colore o un'immagine al valore di un oggetto.
EmptyStringToObjectConverter Converte un valore stringa in un oggetto (se il valore è null o vuoto restituisce il valore false). Può essere usato per associare una visibilità, un colore o un'immagine al valore di una stringa.
<!-- 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
Converte le dimensioni di un file in byte in un formato più leggibile usando 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
Converte una stringa di origine dalle risorse dell'app e ne restituisce il valore, se trovato.
StringFormatConverter
Ciò consente di formattare una proprietà di tipo stringa durante il binding, incapsulando string.Format.
Consente solo un singolo valore di input (stringa di associazione), ma può essere formattato con la stringa normale. Metodi di formato.
<!-- 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
Converte un valore stringa in un valore Visibility (se il valore è null o vuoto restituisce un valore compresso).
<!-- 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
Convertitore che può essere usato per recuperare in modo sicuro i risultati dalle Task<TResult> istanze. Per un esempio, vedere la raccolta di MVVM Toolkit.
TypeToObjectConverter
Restituisce un oggetto o un altro oggetto, a seconda che il tipo del valore specificato corrisponda a un altro tipo specificato.
<!-- 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
Converte un'enumerazione Visibility in un valore booleano.
<!-- 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