SwitchPresenter XAML Control

The SwitchPresenter control acts like a switch statement for XAML. It allows a developer to display certain content based on the condition of another value as an alternative to managing multiple Visibility values or complex visual states.

Unlike traditional approaches of showing/hiding components within a page, the SwitchPresenter will only load and attach the matching Case's content to the Visual Tree.


    <controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=Lookup}">
      <controls:Case Value="Confirmation Code">
          <TextBox Name="ConfirmationCodeValidator"
                   Header="Confirmation code"
                   PlaceholderText="6 letters" />
          <TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=ConfirmationCodeValidator}" Text="Thanks for entering a valid code!" />
      <controls:Case Value="E-ticket number">
          <TextBox Name="TicketValidator"
                   Header="E-ticket number"
                   PlaceholderText="10 or 13 numbers" />
          <TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=TicketValidator}" Text="Thanks for entering a valid code!" />
      <controls:Case Value="Mileage Plan number">
        <TextBox Name="PlanValidator"
                 Header="Mileage Plan #"
                 PlaceholderText="Mileage Plan #" />
      <!-- You can also provide a default case if no match is found -->
      <controls:Case IsDefault="True">
        <TextBlock Text="Please select a way to lookup your reservation above..." />

Sample Project

SwitchPresenter sample page Source. You can see this in action in Windows Community Toolkit Sample App.


Device family Universal, MinVersion 10.0.17763 or higher
Namespace Microsoft.Toolkit.Uwp.UI.Controls
NuGet package Microsoft.Toolkit.Uwp.UI.Controls.Primitives