Created
November 21, 2025 22:54
-
-
Save michael-hawker/7eb67ac1a7d6c25114b39bdd04163cbb to your computer and use it in GitHub Desktop.
Prototype of simple `SelectedContentControl` for housing content similarly to other controls like TabView/FlipView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. | |
| using Windows.UI.Xaml; | |
| using Windows.UI.Xaml.Controls; | |
| namespace CommunityToolkit.WinUI.Controls.Future; | |
| public sealed class SelectedContentControl : ListViewBase // Can't inherit from Selector: https://github.com/microsoft/microsoft-ui-xaml/issues/205 | |
| { | |
| public SelectedContentControl() | |
| { | |
| this.DefaultStyleKey = typeof(SelectedContentControl); | |
| UpdateSelection(); | |
| SelectionChanged += SelectedContentControl_SelectionChanged; | |
| } | |
| protected override bool IsItemItsOwnContainerOverride(object item) => item is SelectedContentItem; | |
| protected override DependencyObject GetContainerForItemOverride() => new SelectedContentItem(); | |
| protected override void PrepareContainerForItemOverride(DependencyObject element, object item) | |
| { | |
| base.PrepareContainerForItemOverride(element, item); | |
| if (element is SelectedContentItem container) | |
| { | |
| container.Visibility = (item == SelectedItem) ? Visibility.Visible : Visibility.Collapsed; | |
| } | |
| } | |
| private void SelectedContentControl_SelectionChanged(object sender, SelectionChangedEventArgs e) | |
| { | |
| foreach (var removedItem in e.RemovedItems) | |
| { | |
| if (ContainerFromItem(removedItem) is SelectedContentItem removedContainer) | |
| { | |
| removedContainer.Visibility = Visibility.Collapsed; | |
| } | |
| } | |
| foreach (var addedItem in e.AddedItems) | |
| { | |
| if (ContainerFromItem(addedItem) is SelectedContentItem addedContainer) | |
| { | |
| addedContainer.Visibility = Visibility.Visible; | |
| } | |
| } | |
| } | |
| protected override void OnItemsChanged(object e) | |
| { | |
| base.OnItemsChanged(e); | |
| // Ensure a valid selection (auto-select first item) | |
| if ((SelectedIndex < 0 && Items.Count > 0) | |
| || SelectedIndex >= Items.Count) | |
| { | |
| SelectedIndex = 0; | |
| } | |
| UpdateSelection(); | |
| } | |
| private void UpdateSelection() | |
| { | |
| // Show only the selected container; hide the rest. | |
| for (int i = 0; i < Items.Count; i++) | |
| { | |
| if (ContainerFromIndex(i) is SelectedContentItem container) | |
| { | |
| container.Visibility = (SelectedIndex == i) ? Visibility.Visible : Visibility.Collapsed; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
| xmlns:controls="using:CommunityToolkit.WinUI.Controls.Future"> | |
| <!-- Item container style --> | |
| <Style x:Key="DefaultSelectedContentItemStyle" TargetType="controls:SelectedContentItem"> | |
| <Setter Property="Padding" Value="0" /> | |
| <Setter Property="HorizontalContentAlignment" Value="Stretch" /> | |
| <Setter Property="VerticalContentAlignment" Value="Stretch" /> | |
| <Setter Property="IsHoldingEnabled" Value="False" /> | |
| <Setter Property="IsTabStop" Value="False" /> | |
| <Setter Property="Template"> | |
| <Setter.Value> | |
| <ControlTemplate TargetType="controls:SelectedContentItem"> | |
| <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" | |
| VerticalAlignment="{TemplateBinding VerticalContentAlignment}" | |
| Content="{TemplateBinding Content}" | |
| ContentTemplate="{TemplateBinding ContentTemplate}" /> | |
| </ControlTemplate> | |
| </Setter.Value> | |
| </Setter> | |
| </Style> | |
| <Style BasedOn="{StaticResource DefaultSelectedContentItemStyle}" TargetType="controls:SelectedContentItem" /> | |
| <!-- Control style --> | |
| <Style x:Key="DefaultSelectedContentControlStyle" TargetType="controls:SelectedContentControl"> | |
| <Setter Property="Background" Value="Transparent" /> | |
| <Setter Property="BorderThickness" Value="0" /> | |
| <Setter Property="CornerRadius" Value="0" /> | |
| <Setter Property="IsSwipeEnabled" Value="False" /> | |
| <Setter Property="IsTabStop" Value="False" /> | |
| <Setter Property="SelectionMode" Value="Single" /> | |
| <Setter Property="TabNavigation" Value="Once" /> | |
| <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" /> | |
| <Setter Property="ItemsPanel"> | |
| <Setter.Value> | |
| <ItemsPanelTemplate> | |
| <Grid /> | |
| </ItemsPanelTemplate> | |
| </Setter.Value> | |
| </Setter> | |
| <Setter Property="Template"> | |
| <Setter.Value> | |
| <ControlTemplate TargetType="controls:SelectedContentControl"> | |
| <ScrollViewer x:Name="ScrollViewer" | |
| Background="{TemplateBinding Background}" | |
| BorderBrush="{TemplateBinding BorderBrush}" | |
| BorderThickness="{TemplateBinding BorderThickness}" | |
| CornerRadius="{TemplateBinding CornerRadius}" | |
| HorizontalScrollBarVisibility="Disabled" | |
| HorizontalScrollMode="Disabled" | |
| IsTabStop="False" | |
| VerticalScrollBarVisibility="Disabled" | |
| VerticalScrollMode="Disabled" | |
| ZoomMode="Disabled"> | |
| <ItemsPresenter /> | |
| </ScrollViewer> | |
| </ControlTemplate> | |
| </Setter.Value> | |
| </Setter> | |
| </Style> | |
| <Style BasedOn="{StaticResource DefaultSelectedContentControlStyle}" TargetType="controls:SelectedContentControl" /> | |
| </ResourceDictionary> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. | |
| using Windows.UI.Xaml.Controls; | |
| namespace CommunityToolkit.WinUI.Controls.Future; | |
| public class SelectedContentItem : ListViewItem | |
| { | |
| public SelectedContentItem() | |
| { | |
| DefaultStyleKey = typeof(SelectedContentItem); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discussion about the need for this and how this came about for XAML Studio in Windows App Community Discord here: https://www.discord.com/channels/372137812037730304/580484470877061120/1433743031764385925
Code was written/tested for UWP, but should work fine for WinUI 3 as well.