-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
280 additions
and
11 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionCode.bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Let's declare a sample enum | ||
public enum Animal | ||
{ | ||
Cat, | ||
Dog, | ||
Bunny, | ||
Parrot, | ||
Squirrel | ||
} | ||
|
||
// We can use a converter to get other values from our enum | ||
public sealed class AnimalToColorConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, string language) | ||
{ | ||
return (Animal)value switch | ||
{ | ||
Animal.Cat => Colors.Coral, | ||
Animal.Dog => Colors.Gray, | ||
Animal.Bunny => Colors.Green, | ||
Animal.Parrot => Colors.YellowGreen, | ||
Animal.Squirrel => Colors.SaddleBrown, | ||
_ => throw new ArgumentException("Invalid value", nameof(value)) | ||
}; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, string language) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.EnumValuesExtensionPage" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions" | ||
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums" | ||
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters" | ||
mc:Ignorable="d"> | ||
<Page.Resources> | ||
<enums:Animal x:Key="MyAnimal">Cat</enums:Animal> | ||
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/> | ||
</Page.Resources> | ||
<Grid> | ||
<StackPanel Spacing="8"> | ||
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}"/> | ||
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}" | ||
SelectedIndex="0"> | ||
<ComboBox.ItemTemplate> | ||
<DataTemplate> | ||
<StackPanel Orientation="Horizontal"> | ||
<TextBlock Text="{Binding}"/> | ||
<Rectangle Height="16" | ||
Width="16"> | ||
<Rectangle.Fill> | ||
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/> | ||
</Rectangle.Fill> | ||
</Rectangle> | ||
</StackPanel> | ||
</DataTemplate> | ||
</ComboBox.ItemTemplate> | ||
</ComboBox> | ||
</StackPanel> | ||
</Grid> | ||
</Page> |
70 changes: 70 additions & 0 deletions
70
...oft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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 System; | ||
using Windows.UI; | ||
using Microsoft.Toolkit.Uwp.UI.Extensions; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Data; | ||
using Microsoft.Toolkit.Uwp.SampleApp.Enums; | ||
|
||
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages | ||
{ | ||
/// <summary> | ||
/// A page that shows how to use the <see cref="EnumValuesExtension"/> type. | ||
/// </summary> | ||
public sealed partial class EnumValuesExtensionPage : IXamlRenderListener | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EnumValuesExtensionPage"/> class. | ||
/// </summary> | ||
public EnumValuesExtensionPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public void OnXamlRendered(FrameworkElement control) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
#pragma warning disable SA1403 // File may only contain a single namespace | ||
namespace Microsoft.Toolkit.Uwp.SampleApp.Enums | ||
{ | ||
public enum Animal | ||
{ | ||
Cat, | ||
Dog, | ||
Bunny, | ||
Parrot, | ||
Squirrel | ||
} | ||
} | ||
|
||
namespace Microsoft.Toolkit.Uwp.SampleApp.Converters | ||
{ | ||
public sealed class AnimalToColorConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, string language) | ||
{ | ||
return (Animal)value switch | ||
{ | ||
Animal.Cat => Colors.Coral, | ||
Animal.Dog => Colors.Gray, | ||
Animal.Bunny => Colors.Green, | ||
Animal.Parrot => Colors.YellowGreen, | ||
Animal.Squirrel => Colors.SaddleBrown, | ||
_ => throw new ArgumentException("Invalid value", nameof(value)) | ||
}; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, string language) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} | ||
|
||
#pragma warning restore SA1403 |
47 changes: 47 additions & 0 deletions
47
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/EnumValuesExtensionXaml.bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<Page | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="using:Microsoft.Toolkit.Uwp.SampleApp.SamplePages.EnumValuesExtensionPage" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions" | ||
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums" | ||
xmlns:converters="using:Microsoft.Toolkit.Uwp.SampleApp.Converters" | ||
mc:Ignorable="d"> | ||
<Page.Resources> | ||
<converters:AnimalToColorConverter x:Key="AnimalToColorConverter"/> | ||
</Page.Resources> | ||
<Grid> | ||
<StackPanel Spacing="8" | ||
HorizontalAlignment="Center" | ||
VerticalAlignment="Center"> | ||
|
||
<!--Simple selector using the default template. Each item will | ||
simply be displayed as the corresponding enum value name.--> | ||
<TextBlock Text="Select an animal:"/> | ||
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}" | ||
SelectedIndex="0"/> | ||
|
||
<!--We can also use a custom template and some converters.--> | ||
<TextBlock Margin="0,12,0,0" | ||
Text="Pick another animal:"/> | ||
<ComboBox ItemsSource="{ex:EnumValues Type=enums:Animal}" | ||
SelectedIndex="0"> | ||
<ComboBox.ItemTemplate> | ||
<DataTemplate> | ||
<Grid Width="80"> | ||
<TextBlock Text="{Binding}"/> | ||
<Rectangle Height="16" | ||
Width="16" | ||
HorizontalAlignment="Right"> | ||
<Rectangle.Fill> | ||
<SolidColorBrush Color="{Binding Converter={StaticResource AnimalToColorConverter}}"/> | ||
</Rectangle.Fill> | ||
</Rectangle> | ||
</Grid> | ||
</DataTemplate> | ||
</ComboBox.ItemTemplate> | ||
</ComboBox> | ||
</StackPanel> | ||
</Grid> | ||
</Page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Microsoft.Toolkit.Uwp.UI/Extensions/Markup/EnumValuesExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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 System; | ||
using Windows.UI.Xaml.Markup; | ||
|
||
namespace Microsoft.Toolkit.Uwp.UI.Extensions | ||
{ | ||
/// <summary> | ||
/// A markup extension that returns a collection of values of a specific <see langword="enum"/> | ||
/// </summary> | ||
[MarkupExtensionReturnType(ReturnType = typeof(Array))] | ||
public sealed class EnumValuesExtension : MarkupExtension | ||
{ | ||
/// <summary> | ||
/// Gets or sets the <see cref="System.Type"/> of the target <see langword="enum"/> | ||
/// </summary> | ||
public Type Type { get; set; } | ||
|
||
/// <inheritdoc/> | ||
protected override object ProvideValue() => Enum.GetValues(Type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
UnitTests/UnitTests.UWP/Extensions/Test_EnumValuesExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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 System; | ||
using Microsoft.Toolkit.Uwp.UI.Extensions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Markup; | ||
|
||
namespace UnitTests.Extensions | ||
{ | ||
[TestClass] | ||
public class Test_EnumValuesExtension | ||
{ | ||
[TestCategory("EnumValuesExtension")] | ||
[UITestMethod] | ||
public void Test_EnumValuesExtension_MarkupExtension() | ||
{ | ||
var treeroot = XamlReader.Load(@"<Page | ||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions"" | ||
xmlns:local=""using:UnitTests.Extensions""> | ||
<ListView x:Name=""Check"" ItemsSource=""{ex:EnumValues Type=local:Animal}""/> | ||
</Page>") as FrameworkElement; | ||
|
||
var list = treeroot.FindChildByName("Check") as ListView; | ||
|
||
Assert.IsNotNull(list, "Could not find listview control in tree."); | ||
|
||
Animal[] items = list.ItemsSource as Animal[]; | ||
|
||
Assert.IsNotNull(items, "The items were not created correctly"); | ||
|
||
CollectionAssert.AreEqual(items, Enum.GetValues(typeof(Animal))); | ||
} | ||
} | ||
|
||
public enum Animal | ||
{ | ||
Cat, | ||
Dog, | ||
Bunny | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters