-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Enable Grid to dynamically switch layouts #4474
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
Draft
nandin-borjigin
wants to merge
4
commits into
CommunityToolkit:main
Choose a base branch
from
nandin-borjigin:grid-dynamic-layout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
115 changes: 115 additions & 0 deletions
115
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/GridExtensions/GridExtensionsCode.bind
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<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 Padding="16" | ||
VerticalAlignment="Top"> | ||
<Grid.RowDefinitions> | ||
<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 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="1" | ||
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 different 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> |
115 changes: 115 additions & 0 deletions
115
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/GridExtensions/GridExtensionsPage.xaml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<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" /> | ||
<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 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="1" | ||
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 different 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> |
16 changes: 16 additions & 0 deletions
16
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/GridExtensions/GridExtensionsPage.xaml.cs
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
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(); | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
Microsoft.Toolkit.Uwp.UI/Extensions/Grid/GridExtensions.ActiveLayout.cs
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
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.