-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathToolkitSampleMultiChoiceOptionAttribute.cs
More file actions
61 lines (55 loc) · 2.82 KB
/
ToolkitSampleMultiChoiceOptionAttribute.cs
File metadata and controls
61 lines (55 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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 CommunityToolkit.Tooling.SampleGen.Attributes;
/// <summary>
/// Generates a property and multi-choice option in the sample option pane that can be used to update it.
/// </summary>
/// <remarks>
/// Using this attribute will automatically generate an <see cref="INotifyPropertyChanged"/>-enabled property
/// that you can bind to in XAML, and displays an options pane alongside your sample which allows the user to manipulate the property.
/// <para/>
/// </remarks>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class ToolkitSampleMultiChoiceOptionAttribute : ToolkitSampleOptionBaseAttribute
{
/// <summary>
/// Creates a new instance of <see cref="ToolkitSampleMultiChoiceOptionAttribute"/>.
/// </summary>
/// <param name="bindingName">The name of the generated property, which you can bind to in XAML.</param>
/// <param name="choices">A list of the choices to display to the user. Can be literal values, or labeled values. Use a " : " separator (single colon surrounded by at least 1 whitespace) to separate a label from a value.</param>
public ToolkitSampleMultiChoiceOptionAttribute(string bindingName, params string[] choices)
: base(bindingName, null)
{
TypeName = "string";
Choices = choices.Select(x =>
{
if (x.Contains(" : "))
{
var parts = x.Split(new string[] { " : " }, StringSplitOptions.RemoveEmptyEntries);
return new MultiChoiceOption(parts[0].TrimEnd(), parts[1].TrimStart());
}
return new MultiChoiceOption(x, x);
}).ToArray();
}
/// <summary>
/// Creates a new instance of <see cref="ToolkitSampleMultiChoiceOptionAttribute"/>.
/// </summary>
/// <param name="bindingName">The name of the generated property, which you can bind to in XAML.</param>
/// <param name="typeName"></param>
/// <param name="choices">A list of the choices to display to the user. Can be literal values, or labeled values. Use a " : " separator (single colon surrounded by at least 1 whitespace) to separate a label from a value.</param>
internal ToolkitSampleMultiChoiceOptionAttribute(string bindingName, string typeName, MultiChoiceOption[] choices)
: base(bindingName, null)
{
TypeName = typeName;
Choices = choices;
}
/// <summary>
/// A collection of choices to display in the options pane.
/// </summary>
public MultiChoiceOption[] Choices { get; }
/// <summary>
/// The source generator-friendly type name used for casting.
/// </summary>
internal override string TypeName { get; }
}