Skip to content

Commit 41065bc

Browse files
committed
Added second sample with a converter
1 parent 2279200 commit 41065bc

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<AppxBundlePlatforms>x86|x64|arm|arm64</AppxBundlePlatforms>
2222
<Win32Resource>MiddleClickScrolling-CursorType.res</Win32Resource>
2323
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
24+
<LangVersion>8.0</LangVersion>
2425
</PropertyGroup>
2526
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
2627
<DebugSymbols>true</DebugSymbols>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionCode.bind

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,26 @@ public enum Animal
66
Bunny,
77
Parrot,
88
Squirrel
9+
}
10+
11+
// We can use a converter to get other values from our enum
12+
public sealed class AnimalToColorConverter : IValueConverter
13+
{
14+
public object Convert(object value, Type targetType, object parameter, string language)
15+
{
16+
return (Animal)value switch
17+
{
18+
Animal.Cat => Colors.Coral,
19+
Animal.Dog => Colors.Gray,
20+
Animal.Bunny => Colors.Green,
21+
Animal.Parrot => Colors.YellowGreen,
22+
Animal.Squirrel => Colors.SaddleBrown,
23+
_ => throw new ArgumentException("Invalid value", nameof(value))
24+
};
25+
}
26+
27+
public object ConvertBack(object value, Type targetType, object parameter, string language)
28+
{
29+
throw new NotImplementedException();
30+
}
931
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionPage.xaml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,32 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
7-
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages.Enums"
7+
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
8+
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters"
89
mc:Ignorable="d">
910
<Page.Resources>
1011
<enums:Animal x:Key="MyAnimal">Cat</enums:Animal>
12+
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/>
1113
</Page.Resources>
1214
<Grid>
1315
<StackPanel Spacing="8">
14-
<TextBlock Text="Select an animal:"/>
1516
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"/>
17+
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
18+
SelectedIndex="0">
19+
<ComboBox.ItemTemplate>
20+
<DataTemplate>
21+
<StackPanel Orientation="Horizontal">
22+
<TextBlock Text="{Binding}"/>
23+
<Rectangle Height="16"
24+
Width="16">
25+
<Rectangle.Fill>
26+
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/>
27+
</Rectangle.Fill>
28+
</Rectangle>
29+
</StackPanel>
30+
</DataTemplate>
31+
</ComboBox.ItemTemplate>
32+
</ComboBox>
1633
</StackPanel>
1734
</Grid>
1835
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionPage.xaml.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using Windows.UI;
57
using Microsoft.Toolkit.Uwp.UI.Extensions;
68
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Data;
10+
using Microsoft.Toolkit.Uwp.SampleApp.Enums;
711

812
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
913
{
@@ -27,7 +31,7 @@ public void OnXamlRendered(FrameworkElement control)
2731
}
2832

2933
#pragma warning disable SA1403 // File may only contain a single namespace
30-
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages.Enums
34+
namespace Microsoft.Toolkit.Uwp.SampleApp.Enums
3135
{
3236
public enum Animal
3337
{
@@ -39,4 +43,28 @@ public enum Animal
3943
}
4044
}
4145

46+
namespace Microsoft.Toolkit.Uwp.SampleApp.Converters
47+
{
48+
public sealed class AnimalToColorConverter : IValueConverter
49+
{
50+
public object Convert(object value, Type targetType, object parameter, string language)
51+
{
52+
return (Animal)value switch
53+
{
54+
Animal.Cat => Colors.Coral,
55+
Animal.Dog => Colors.Gray,
56+
Animal.Bunny => Colors.Green,
57+
Animal.Parrot => Colors.YellowGreen,
58+
Animal.Squirrel => Colors.SaddleBrown,
59+
_ => throw new ArgumentException("Invalid value", nameof(value))
60+
};
61+
}
62+
63+
public object ConvertBack(object value, Type targetType, object parameter, string language)
64+
{
65+
throw new NotImplementedException();
66+
}
67+
}
68+
}
69+
4270
#pragma warning restore SA1403

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionXaml.bind

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
8-
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages.Enums"
8+
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
9+
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters"
910
mc:Ignorable="d">
11+
<Page.Resources>
12+
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/>
13+
</Page.Resources>
1014
<Grid>
1115
<StackPanel Spacing="8"
1216
HorizontalAlignment="Center"
@@ -17,6 +21,27 @@
1721
<TextBlock Text="Select an animal:"/>
1822
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
1923
SelectedIndex="0"/>
24+
25+
<!--We can also use a custom template and some converters.-->
26+
<TextBlock Margin="0,12,0,0"
27+
Text="Pick another animal:"/>
28+
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"
29+
SelectedIndex="0">
30+
<ComboBox.ItemTemplate>
31+
<DataTemplate>
32+
<Grid Width="80">
33+
<TextBlock Text="{Binding}"/>
34+
<Rectangle Height="16"
35+
Width="16"
36+
HorizontalAlignment="Right">
37+
<Rectangle.Fill>
38+
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/>
39+
</Rectangle.Fill>
40+
</Rectangle>
41+
</Grid>
42+
</DataTemplate>
43+
</ComboBox.ItemTemplate>
44+
</ComboBox>
2045
</StackPanel>
2146
</Grid>
2247
</Page>

0 commit comments

Comments
 (0)