Skip to content

Enum values markdown extension #3357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Sergio0694
Copy link
Member

@Sergio0694 Sergio0694 commented Jun 22, 2020

PR Type

What kind of change does this PR introduce?

  • Feature

What is the new behavior?

This PR adds a new EnumValuesExtension type, a markup extension that can be used to easily assign the list of possible enum values to an ItemsSource property. This is especially useful eg. when using the ComboBox control to display a list of available settings. Consider the following example for a settings page in an app:

xmlns:ex="using:Microsoft.Toolkit.Uwp.UI.Extensions"
xmlns:enums="using:MyApp.Enums"

<StackPanel Spacing="8">
    <TextBlock Text="Pick one"/>
    <ComboBox ItemsSource="{ex:EnumValues Type=enums:MyEnum}"/>
</StackPanel>

The result is an easy to use collection of values that can be displayed in a variety of ways by tweaking the data template in use. It's also possible to easily localize enum values by using a convention-based pattern for the resource strings. For instance, in my apps I localize enum values using the [ENUM_TYPE]/[ENUM_VALUE] format. In this case, I simply bind the Text property of my template to a convert that grabs the right localization string with that value. (on this note, @michael-hawker let me know if you'd also like me to open a PR to add that converter to the toolkit). Here's a GIF as a reference for the result of this extension:

8g2VOk5Qq4

You can see I'm showing a variety of different visual styles for the various selectors, just by changing the data template. All of them are using this extension to load the enum values to display, instead of having a redundant collection property in the viewmodel.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions. Link:
  • Sample in sample app has been added / updated (for bug fixes / features)
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

@ghost
Copy link

ghost commented Jun 22, 2020

Thanks Sergio0694 for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost assigned Kyaa-dost Jun 22, 2020
@Kyaa-dost
Copy link
Contributor

@Sergio0694 Toolkit-CI is failing here.

image

@Sergio0694
Copy link
Member Author

@Kyaa-dost *sigh*... .NET Native strikes again 🤣

Those are passing locally, I'll try adding the type to the .rd.xml file and see if that fixes things.
Thanks for the ping!

@Sergio0694
Copy link
Member Author

Well that didn't work. Uhm... 🤔

Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add the types in the UnitTestApp.xaml file to not get optimized out:

        <extensions:EnumValuesExtension x:Key="DummyExtension"/>

        <unitTestExtensions:Animal x:Key="Animal">Cat</unitTestExtensions:Animal>

We should also have a sample page to show advanced scenarios like DataTemplates (simple and with extra data) and also things like doing conversions on names/etc...?

@ghost
Copy link

ghost commented Jun 25, 2020

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

@Sergio0694
Copy link
Member Author

Added those tweaks in 4b7eb2a, thanks! 😄

Will work on a sample page then 🚀

Copy link
Contributor

@vgromfeld vgromfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@michael-hawker michael-hawker merged commit 9e2ae30 into CommunityToolkit:master Jul 10, 2020
@michael-hawker michael-hawker added this to the 7.0 milestone Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants