-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add basic SwitchPresenter Sample #3789
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
Changes from all commits
9bbe69c
2506faa
c63e695
332fd2c
08bbe58
2e3ccec
bb961a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ public enum Animal | |
| Cat, | ||
| Dog, | ||
| Bunny, | ||
| Llama, | ||
| Parrot, | ||
| Squirrel | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <Page | ||
| 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:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" | ||
| xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" | ||
| xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums" | ||
| mc:Ignorable="d"> | ||
|
|
||
| <StackPanel Padding="16"> | ||
| <!-- Basic Sample --> | ||
| <ComboBox x:Name="Lookup" Header="Look up reservation" SelectedIndex="0" | ||
| Margin="0,0,0,8"> | ||
| <x:String>Select an option</x:String> | ||
| <x:String>Confirmation Code</x:String> | ||
| <x:String>E-ticket number</x:String> | ||
| <x:String>Mileage Plan number</x:String> | ||
| </ComboBox> | ||
| <!-- SwitchPresenter binds to a value --> | ||
| <controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=Lookup}"> | ||
| <!-- And then only dynamically displays the Case with the matching Value --> | ||
| <controls:Case Value="Confirmation Code"> | ||
| <StackPanel> | ||
| <TextBox Name="ConfirmationCodeValidator" | ||
| ui:TextBoxExtensions.Regex="^[a-zA-Z]{6}$" | ||
| Header="Confirmation code" | ||
| PlaceholderText="6 letters" /> | ||
| <TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=ConfirmationCodeValidator}">Thanks for entering a valid code!</TextBlock> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use Text property for fast path rendering |
||
| </StackPanel> | ||
| </controls:Case> | ||
| <controls:Case Value="E-ticket number"> | ||
| <StackPanel> | ||
| <TextBox Name="TicketValidator" | ||
| ui:TextBoxExtensions.Regex="(^\d{10}$)|(^\d{13}$)" | ||
| Header="E-ticket number" | ||
| PlaceholderText="10 or 13 numbers" /> | ||
| <TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=TicketValidator}">Thanks for entering a valid code!</TextBlock> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: same here |
||
| </StackPanel> | ||
| </controls:Case> | ||
| <controls:Case Value="Mileage Plan number"> | ||
| <TextBox Name="PlanValidator" | ||
| Header="Mileage Plan #" | ||
| PlaceholderText="Mileage Plan #" /> | ||
| </controls:Case> | ||
| <!-- You can also provide a default case if no match is found --> | ||
| <controls:Case IsDefault="True"> | ||
| <TextBlock>Please select a way to lookup your reservation above...</TextBlock> | ||
| </controls:Case> | ||
| </controls:SwitchPresenter> | ||
|
|
||
| <Border Height="2" Background="Gray" Margin="0,16"/> | ||
|
|
||
| <!-- Scenario using an Enum --> | ||
| <ComboBox x:Name="AnimalPicker" | ||
| Header="Pick an Animal" | ||
| ItemsSource="{ui:EnumValues Type=enums:Animal}" | ||
| SelectedIndex="0"/> | ||
| <controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=AnimalPicker}" | ||
| TargetType="enums:Animal" | ||
| Padding="16"> | ||
| <controls:Case Value="Cat"> | ||
| <TextBlock FontSize="32">🐈</TextBlock> | ||
| </controls:Case> | ||
| <controls:Case Value="Dog"> | ||
| <TextBlock FontSize="32">🐕</TextBlock> | ||
| </controls:Case> | ||
| <controls:Case Value="Bunny"> | ||
| <TextBlock FontSize="32">🐇</TextBlock> | ||
| </controls:Case> | ||
| <controls:Case Value="Llama"> | ||
| <TextBlock FontSize="32">🦙</TextBlock> | ||
michael-hawker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </controls:Case> | ||
| <controls:Case Value="Parrot"> | ||
| <TextBlock FontSize="32">🦜</TextBlock> | ||
| </controls:Case> | ||
| <controls:Case Value="Squirrel"> | ||
| <TextBlock FontSize="32">🐿</TextBlock> | ||
| </controls:Case> | ||
| </controls:SwitchPresenter> | ||
| </StackPanel> | ||
| </Page> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,130 +5,20 @@ | |
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using Windows.UI.Xaml; | ||
|
|
||
| namespace Microsoft.Toolkit.Uwp.UI.Controls | ||
| { | ||
| /// <summary> | ||
| /// An collection of <see cref="Case"/> to help with XAML interop. | ||
| /// </summary> | ||
| public class CaseCollection : IList<Case>, IEnumerable<Case> // TODO: Do we need this or can we use an ObservableCollection directly??? (Or is it useful to have it manage the registration of the child events?) | ||
| public class CaseCollection : DependencyObjectCollection | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this not sealed on purpose? |
||
| { | ||
| internal SwitchPresenter Parent { get; set; } // TODO: Can we remove Parent need here and just use events? | ||
|
|
||
| private readonly List<Case> _internalList = new List<Case>(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public int Count => _internalList.Count; | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool IsReadOnly => false; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Case this[int index] { get => _internalList[index]; set => Insert(index, value); } | ||
|
|
||
| /// <summary> | ||
| /// Raised when an animation has been added/removed or modified | ||
| /// </summary> | ||
| public event EventHandler CaseCollectionChanged; | ||
|
|
||
| private void ValueChanged(object sender, EventArgs e) | ||
| { | ||
| CaseCollectionChanged?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CaseCollection"/> class. | ||
| /// </summary> | ||
| public CaseCollection() | ||
| { | ||
| } | ||
|
Comment on lines
17
to
22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this could safely be removed in theory, right? |
||
|
|
||
| /// <inheritdoc/> | ||
| public int IndexOf(Case item) | ||
| { | ||
| return _internalList.IndexOf(item); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Insert(int index, Case item) | ||
| { | ||
| item.ValueChanged += ValueChanged; | ||
| item.Parent = Parent; | ||
| _internalList.Insert(index, item); | ||
| CaseCollectionChanged?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void RemoveAt(int index) | ||
| { | ||
| if (index >= 0 && index < _internalList.Count) | ||
| { | ||
| var xcase = _internalList[index]; | ||
| xcase.ValueChanged -= ValueChanged; | ||
| xcase.Parent = null; | ||
| } | ||
|
|
||
| _internalList.RemoveAt(index); | ||
| CaseCollectionChanged?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Add(Case item) | ||
| { | ||
| item.ValueChanged += ValueChanged; | ||
| item.Parent = Parent; | ||
| _internalList.Add(item); | ||
| CaseCollectionChanged?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Clear() | ||
| { | ||
| foreach (var xcase in _internalList) | ||
| { | ||
| xcase.ValueChanged -= ValueChanged; | ||
| xcase.Parent = null; | ||
| } | ||
|
|
||
| _internalList.Clear(); | ||
| CaseCollectionChanged?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Contains(Case item) | ||
| { | ||
| return _internalList.Contains(item); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void CopyTo(Case[] array, int arrayIndex) | ||
| { | ||
| _internalList.CopyTo(array, arrayIndex); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool Remove(Case item) | ||
| { | ||
| var result = _internalList.Remove(item); | ||
| if (result) | ||
| { | ||
| item.ValueChanged -= ValueChanged; | ||
| item.Parent = null; | ||
| CaseCollectionChanged?.Invoke(this, EventArgs.Empty); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public IEnumerator<Case> GetEnumerator() | ||
| { | ||
| return _internalList.GetEnumerator(); | ||
| } | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() | ||
| { | ||
| return _internalList.GetEnumerator(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most important change in this PR :P