Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<Content Include="Icons\More.png" />
<Content Include="Icons\Notifications.png" />
<Content Include="Icons\Services.png" />
<Content Include="SamplePages\Primitives\SwitchPresenter.png" />
<Content Include="SamplePages\TabbedCommandBar\TabbedCommandBar.png" />
<Content Include="SamplePages\Animations\Effects\FadeBehavior.png" />
<Content Include="SamplePages\ColorPicker\ColorPicker.png" />
Expand Down Expand Up @@ -976,6 +977,9 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Content Include="SamplePages\Primitives\SwitchPresenter.bind">
<SubType>Designer</SubType>
</Content>
<Page Include="SamplePages\TilesBrush\TilesBrushPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum Animal
Cat,
Dog,
Bunny,
Llama,
Copy link
Contributor

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

Parrot,
Squirrel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public object Convert(object value, Type targetType, object parameter, string la
Animal.Cat => Colors.Coral,
Animal.Dog => Colors.Gray,
Animal.Bunny => Colors.Green,
Animal.Llama => Colors.Beige,
Animal.Parrot => Colors.YellowGreen,
Animal.Squirrel => Colors.SaddleBrown,
_ => throw new ArgumentException("Invalid value", nameof(value))
Expand Down
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>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
</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>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@
</Grid>
</DataTemplate>
<Style TargetType="ListViewItem">
<!-- Change those values to change the WrapPanel's children alignment -->
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="MinHeight" Value="0" />
</Style>
</Page.Resources>

Expand All @@ -49,16 +54,6 @@
HorizontalSpacing="@[HorizontalSpacing:Slider:5:0-200]@" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<!-- Change those values to change the WrapPanel's children alignment -->
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="0" />
<Setter Property="MinWidth" Value="0" />
<Setter Property="MinHeight" Value="0" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</Page>
6 changes: 6 additions & 0 deletions Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations"
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactions="using:Microsoft.Xaml.Interactions.Core"
Expand Down Expand Up @@ -34,6 +35,11 @@
<ui:SurfaceDialOptions />
</ui:TextBoxExtensions.SurfaceDialOptions>
</TextBox>
<controls:SwitchPresenter x:Key="SwitchPresenterControl">
<controls:CaseCollection>
<controls:Case IsDefault="True" />
</controls:CaseCollection>
</controls:SwitchPresenter>
</Page.Resources>

<Grid>
Expand Down
9 changes: 9 additions & 0 deletions Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,15 @@
"XamlCodeFile": "/SamplePages/TabbedCommandBar/TabbedCommandBar.bind",
"Icon": "/SamplePages/TabbedCommandBar/TabbedCommandBar.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/TabbedCommandBar.md"
},
{
"Name": "SwitchPresenter",
"Subcategory": "Layout",
"About": "The SwitchPresenter is a ContentPresenter which can allow a developer to mimic a switch statement within XAML.",
"CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter",
"XamlCodeFile": "/SamplePages/Primitives/SwitchPresenter.bind",
"Icon": "/SamplePages/Primitives/SwitchPresenter.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/SwitchPresenter.md"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
[ContentProperty(Name = nameof(Content))]
public partial class Case : DependencyObject
{
internal SwitchPresenter Parent { get; set; } // TODO: Can we remove Parent need here and just use events?

/// <summary>
/// Event raised when the <see cref="Value"/> property changes.
/// </summary>
public event EventHandler ValueChanged;

/// <summary>
/// Gets or sets the Content to display when this case is active.
/// </summary>
Expand Down Expand Up @@ -64,14 +57,7 @@ public object Value
/// Identifies the <see cref="Value"/> property.
/// </summary>
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(object), typeof(Case), new PropertyMetadata(null, OnValuePropertyChanged));

private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var xcase = (Case)d;

xcase.ValueChanged?.Invoke(xcase, EventArgs.Empty);
}
DependencyProperty.Register(nameof(Value), typeof(object), typeof(Case), new PropertyMetadata(null));

/// <summary>
/// Initializes a new instance of the <see cref="Case"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
}
}
Loading