Skip to content
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

feat: Enable Grid to dynamically switch layouts #4474

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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 @@ -512,6 +512,9 @@
<Compile Include="SamplePages\FocusBehavior\FocusBehaviorPage.xaml.cs">
<DependentUpon>FocusBehaviorPage.xaml</DependentUpon>
</Compile>
<Compile Include="SamplePages\GridExtensions\GridExtensionsPage.xaml.cs">
<DependentUpon>GridExtensionsPage.xaml</DependentUpon>
</Compile>
<Compile Include="SamplePages\MetadataControl\MetadataControlPage.xaml.cs">
<DependentUpon>MetadataControlPage.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -647,6 +650,7 @@
<Content Include="SamplePages\Animations\Shadows\AnimatedCardShadowXaml.bind" />
<Content Include="SamplePages\KeyDownTriggerBehavior\KeyDownTriggerBehaviorXaml.bind" />
<Content Include="SamplePages\RichSuggestBox\RichSuggestBoxCode.bind" />
<Content Include="SamplePages\GridExtensions\GridExtensionsCode.bind" />
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
Expand Down Expand Up @@ -991,6 +995,10 @@
<Content Include="SamplePages\MetadataControl\MetadataControlCode.bind">
<SubType>Designer</SubType>
</Content>
<Page Include="SamplePages\GridExtensions\GridExtensionsPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="SamplePages\MetadataControl\MetadataControlPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<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:ui="using:Microsoft.Toolkit.Uwp.UI"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:triggers="using:Microsoft.Toolkit.Uwp.UI.Triggers"
mc:Ignorable="d">

<Page.Resources>
<Style TargetType="Border">
<Setter Property="Background" Value="Gray" />
<Setter Property="Padding" Value="4" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</Page.Resources>

<Grid VerticalAlignment="Top" Padding="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
nandin-borjigin marked this conversation as resolved.
Show resolved Hide resolved
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<triggers:CompareStateTrigger
Value="{Binding ElementName=Resizer, Path=(ui:FrameworkElementExtensions.ActualWidth)}"
Comparison="LessThanOrEqual"
To="360"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="DynamicLayoutGrid.(ui:GridExtensions.ActiveLayout)" Value="Narrow"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>


<Grid Grid.Row="1" Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="240" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border
x:Name="Resizer"
ui:FrameworkElementExtensions.EnableActualSizeBinding="True"
Background="{StaticResource Brush-Brand-Color}">
<TextBlock TextWrapping="Wrap">
Resize to see the layout to be <LineBreak/>
switched dynamically.
</TextBlock>
</Border>
<controls:GridSplitter
HorizontalAlignment="Left"
Grid.Column="1"
Width="12"/>
</Grid>

<Grid
Grid.Row="2"
x:Name="DynamicLayoutGrid"
ui:GridExtensions.ActiveLayout="Normal"
ColumnSpacing="12"
RowSpacing="8"
BorderThickness="1"
BorderBrush="{ThemeResource SystemControlHighlightChromeHighBrush}"
HorizontalAlignment="Left"
Width="{Binding ElementName=Resizer, Path=(ui:FrameworkElementExtensions.ActualWidth)}"
Padding="8">
<!-- Declaratively define the possible layouts. -->
<!-- GridEx.Layouts is a dictionary of GridLayoutDefinition -->
<ui:GridExtensions.Layouts>
<ui:GridLayoutDefinition x:Key="Normal">
<!-- A GridLayoutDefinition consists of -->
<!-- row definitions, column definitions and an area definition -->
<ui:GridLayoutDefinition.RowDefinitions>
<RowDefinition Height="Auto"/>
</ui:GridLayoutDefinition.RowDefinitions>
<ui:GridLayoutDefinition.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</ui:GridLayoutDefinition.ColumnDefinitions>
<!-- Area definition just simply puts down -->
<!-- children names in desired order -->
Number Title Description
</ui:GridLayoutDefinition>
<ui:GridLayoutDefinition x:Key="Narrow">
<ui:GridLayoutDefinition.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</ui:GridLayoutDefinition.RowDefinitions>
<ui:GridLayoutDefinition.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</ui:GridLayoutDefinition.ColumnDefinitions>
Number Title; <!-- semicolon is used to separate differnt rows -->
Description Description <!-- row/column span is expressed by repeating the elment name -->
</ui:GridLayoutDefinition>
</ui:GridExtensions.Layouts>
<Border x:Name="Number" Width="32">
<TextBlock>1</TextBlock>
</Border>
<Border x:Name="Title">
<TextBlock>Lorem Ipsum</TextBlock>
</Border>
<Border x:Name="Description">
<TextBlock>Lorem ipsum dolor sit amet...</TextBlock>
</Border>
</Grid>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<Page x:Class="Microsoft.Toolkit.Uwp.SampleApp.SamplePages.GridExtensionsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:triggers="using:Microsoft.Toolkit.Uwp.UI.Triggers"
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
mc:Ignorable="d">

<Page.Resources>
<Style TargetType="Border">
<Setter Property="Background" Value="Gray" />
<Setter Property="Padding" Value="4" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Page.Resources>

<Grid Padding="16"
VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
nandin-borjigin marked this conversation as resolved.
Show resolved Hide resolved
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<triggers:CompareStateTrigger Comparison="LessThanOrEqual"
Value="{Binding ElementName=Resizer, Path=(ui:FrameworkElementExtensions.ActualWidth)}"
To="360" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="DynamicLayoutGrid.(ui:GridExtensions.ActiveLayout)" Value="Narrow" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>


<Grid Grid.Row="1"
nandin-borjigin marked this conversation as resolved.
Show resolved Hide resolved
Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="240" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border x:Name="Resizer"
ui:FrameworkElementExtensions.EnableActualSizeBinding="True"
Background="{StaticResource Brush-Brand-Color}">
<TextBlock TextWrapping="Wrap">
Resize to see the layout to be<LineBreak />
switched dynamically.</TextBlock>
</Border>
<controls:GridSplitter Grid.Column="1"
Width="12"
HorizontalAlignment="Left" />
</Grid>

<Grid x:Name="DynamicLayoutGrid"
Grid.Row="2"
nandin-borjigin marked this conversation as resolved.
Show resolved Hide resolved
Width="{Binding ElementName=Resizer, Path=(ui:FrameworkElementExtensions.ActualWidth)}"
Padding="8"
HorizontalAlignment="Left"
ui:GridExtensions.ActiveLayout="Normal"
BorderBrush="{ThemeResource SystemControlHighlightChromeHighBrush}"
BorderThickness="1"
ColumnSpacing="12"
RowSpacing="8">
<!-- Declaratively define the possible layouts. -->
<!-- GridEx.Layouts is a dictionary of GridLayoutDefinition -->
<ui:GridExtensions.Layouts>
<ui:GridLayoutDefinition x:Key="Normal">
<!-- A GridLayoutDefinition consists of -->
<!-- row definitions, column definitions and an area definition -->
<ui:GridLayoutDefinition.RowDefinitions>
<RowDefinition Height="Auto" />
</ui:GridLayoutDefinition.RowDefinitions>
<ui:GridLayoutDefinition.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</ui:GridLayoutDefinition.ColumnDefinitions>
<!-- Area definition just simply puts down -->
<!-- children names in desired order -->
Number Title Description
</ui:GridLayoutDefinition>
<ui:GridLayoutDefinition x:Key="Narrow">
<ui:GridLayoutDefinition.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</ui:GridLayoutDefinition.RowDefinitions>
<ui:GridLayoutDefinition.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</ui:GridLayoutDefinition.ColumnDefinitions>
Number Title;
<!-- semicolon is used to separate differnt rows -->
nandin-borjigin marked this conversation as resolved.
Show resolved Hide resolved
Description Description
<!-- row/column span is expressed by repeating the elment name -->
</ui:GridLayoutDefinition>
</ui:GridExtensions.Layouts>
<Border x:Name="Number"
Width="32">
<TextBlock>1</TextBlock>
</Border>
<Border x:Name="Title">
<TextBlock>Lorem Ipsum</TextBlock>
</Border>
<Border x:Name="Description">
<TextBlock>Lorem ipsum dolor sit amet...</TextBlock>
</Border>
</Grid>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 Microsoft.Toolkit.Uwp.SampleApp.SamplePages
{
public sealed partial class GridExtensionsPage : Page
{
public GridExtensionsPage()
{
this.InitializeComponent();
}
}
}
17 changes: 12 additions & 5 deletions Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,14 @@
"XamlCodeFile": "EnumValuesExtensionXaml.bind",
"CodeFile": "EnumValuesExtensionCode.bind",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/extensions/EnumValuesExtension.md"
},
{
"Name": "GridExtensions",
"Type": "GridExtensionsPage",
"About": "Extensions to enable switching grid layouts dynamically",
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI/Extensions/GridExtensions",
"XamlCodeFile": "GridExtensionsCode.bind",
"Icon": "/Assets/Helpers.png"
}
]
},
Expand All @@ -1302,7 +1310,7 @@
"About": "Demonstrate the properties and events of the Gaze Interaction library",
"XamlCodeFile": "GazeInteractionXaml.bind",
"CodeFile": "GazeInteractionCode.bind",
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.Input.GazeInteraction",
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.Input.GazeInteraction",
"Icon": "/SamplePages/GazeInteraction/GazeInteraction.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/gaze/GazeInteractionLibrary.md",
"ApiCheck": "Windows.Devices.Input.Preview.GazeInputSourcePreview"
Expand All @@ -1311,13 +1319,12 @@
"Name": "GazeTracing",
"Type": "GazeTracingPage",
"About": "Shows how to use the Windows 10 API for eye trackers",
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.Input.GazeInteraction",
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.Input.GazeInteraction",
"XamlCodeFile": "GazeTracingXaml.bind",
"CodeFile": "GazeTracingCode.bind",
"Icon": "/SamplePages/GazeTracing/GazeTracing.png",
"ApiCheck": "Windows.Devices.Input.Preview.GazeInputSourcePreview",
"DocumentationUrl" : "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/gaze/GazeInteractionLibrary.md"
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/gaze/GazeInteractionLibrary.md"
}
]
}
]
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 Microsoft.Toolkit.Uwp.UI
{
/// <summary>
/// Provides ActiveLayout attached property for <see cref="Grid"/> element.
/// </summary>
public static partial class GridExtensions
{
/// <summary>
/// Attached <see cref="DependencyProperty"/> for binding <see cref="ActiveLayoutProperty"/> to a <see cref="Grid"/>
/// </summary>
public static readonly DependencyProperty ActiveLayoutProperty =
DependencyProperty.RegisterAttached("ActiveLayout", typeof(string), typeof(GridExtensions), new PropertyMetadata(null, OnActiveLayoutChanged));

/// <summary>
/// Gets the <see cref="ActiveLayoutProperty"/> associated with the specified <see cref="Grid"/>
/// </summary>
/// <param name="obj">The <see cref="Windows.UI.Xaml.Controls.Grid"/> from which to get the associated <see cref="ActiveLayoutProperty"/> value</param>
/// <returns>The <see cref="ActiveLayoutProperty"/> value associated with the <see cref="Grid"/> or null</returns>
public static string GetActiveLayout(Grid obj) => (string)obj.GetValue(ActiveLayoutProperty);

/// <summary>
/// Sets the <see cref="ActiveLayoutProperty"/> associated with the specified <see cref="Grid"/>
/// </summary>
/// <param name="obj">The <see cref="Grid"/> to associated the <see cref="ActiveLayoutProperty"/> to</param>
/// <param name="value">The <see cref="ActiveLayoutProperty"/> to bind to the <see cref="Grid"/></param>
public static void SetActiveLayout(Grid obj, string value) => obj.SetValue(ActiveLayoutProperty, value);

private static void OnActiveLayoutChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Grid grid)
{
UpdateLayout(grid);
}
}
}
}
Loading