-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,164 @@ | ||
using Microsoft.UI.Xaml.Markup; | ||
|
||
namespace DevWinUI; | ||
public partial class ElementGroup : ItemsControl | ||
{ | ||
private Dictionary<Control, long> _visibilityCallbacks = new(); | ||
|
||
public static readonly DependencyProperty OrientationProperty = | ||
DependencyProperty.Register(nameof(Orientation), typeof(Orientation), typeof(ElementGroup), new PropertyMetadata(Orientation.Horizontal, OnOrientationChanged)); | ||
public Orientation Orientation | ||
{ | ||
get => (Orientation)GetValue(OrientationProperty); | ||
set => SetValue(OrientationProperty, value); | ||
} | ||
private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
var ctl = (ElementGroup)d; | ||
if (ctl != null) | ||
{ | ||
ctl.UpdateOrientation(); | ||
} | ||
} | ||
|
||
private void UpdateOrientation() | ||
{ | ||
if (Orientation == Orientation.Horizontal) | ||
{ | ||
ItemsPanel = (ItemsPanelTemplate)XamlReader.Load("<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><StackPanel Orientation='Horizontal'/></ItemsPanelTemplate>"); | ||
} | ||
else | ||
{ | ||
ItemsPanel = (ItemsPanelTemplate)XamlReader.Load("<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><StackPanel Orientation='Vertical'/></ItemsPanelTemplate>"); | ||
} | ||
UpdateElements(); | ||
} | ||
|
||
public ElementGroup() | ||
{ | ||
UpdateOrientation(); | ||
} | ||
protected override void OnApplyTemplate() | ||
{ | ||
base.OnApplyTemplate(); | ||
|
||
UpdateElements(); | ||
Loaded -= OnLoaded; | ||
Loaded += OnLoaded; | ||
} | ||
|
||
private void OnLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
UpdateVisibilityEventHandlers(); | ||
UpdateElements(); | ||
} | ||
|
||
protected override void OnItemsChanged(object e) | ||
{ | ||
base.OnItemsChanged(e); | ||
|
||
UpdateVisibilityEventHandlers(); | ||
UpdateElements(); | ||
} | ||
private void UpdateVisibilityEventHandlers() | ||
{ | ||
if (ItemsPanelRoot is Panel panel) | ||
{ | ||
foreach (var entry in _visibilityCallbacks) | ||
{ | ||
entry.Key.UnregisterPropertyChangedCallback(VisibilityProperty, entry.Value); | ||
} | ||
_visibilityCallbacks.Clear(); | ||
|
||
foreach (var child in panel.Children) | ||
{ | ||
if (child is Control element) | ||
{ | ||
long token = element.RegisterPropertyChangedCallback(VisibilityProperty, OnChildVisibilityChanged); | ||
_visibilityCallbacks[element] = token; | ||
} | ||
} | ||
} | ||
} | ||
private void OnChildVisibilityChanged(DependencyObject sender, DependencyProperty dp) | ||
{ | ||
if (dp == VisibilityProperty) | ||
{ | ||
UpdateElements(); | ||
} | ||
} | ||
private void UpdateElements() | ||
{ | ||
if (ItemsPanelRoot is Panel panel) | ||
{ | ||
var visibleChildren = panel.Children | ||
.OfType<Control>() | ||
.Where(c => c.Visibility == Visibility.Visible) | ||
.ToList(); | ||
|
||
int totalItems = visibleChildren.Count; | ||
|
||
if (totalItems == 1) | ||
{ | ||
if (visibleChildren[0] is Control element) | ||
{ | ||
element.CornerRadius = new CornerRadius(4); | ||
element.BorderThickness = new Thickness(1); | ||
} | ||
return; | ||
} | ||
|
||
for (int i = 0; i < totalItems; i++) | ||
{ | ||
if (visibleChildren[i] is Control element) | ||
{ | ||
if (Orientation == Orientation.Horizontal) | ||
{ | ||
SetStackPanelHorizontalItems(i, totalItems, element); | ||
} | ||
else | ||
{ | ||
SetStackPanelVerticalItems(i, totalItems, element); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void SetStackPanelVerticalItems(int index, int totalItems, Control element) | ||
{ | ||
if (index == 0) | ||
{ | ||
element.CornerRadius = new CornerRadius(4, 4, 0, 0); | ||
element.BorderThickness = new Thickness(1, 1, 1, 0); | ||
} | ||
else if (index == totalItems - 1) | ||
{ | ||
element.CornerRadius = new CornerRadius(0, 0, 4, 4); | ||
element.BorderThickness = new Thickness(1, 1, 1, 1); | ||
} | ||
else | ||
{ | ||
element.CornerRadius = new CornerRadius(0); | ||
element.BorderThickness = new Thickness(1, 1, 1, 0); | ||
} | ||
} | ||
private void SetStackPanelHorizontalItems(int index, int totalItems, Control element) | ||
{ | ||
if (index == 0) | ||
{ | ||
element.CornerRadius = new CornerRadius(4, 0, 0, 4); | ||
element.BorderThickness = new Thickness(1, 1, 0, 1); | ||
} | ||
else if (index == totalItems - 1) | ||
{ | ||
element.CornerRadius = new CornerRadius(0, 4, 4, 0); | ||
element.BorderThickness = new Thickness(1, 1, 1, 1); | ||
} | ||
else | ||
{ | ||
element.CornerRadius = new CornerRadius(0); | ||
element.BorderThickness = new Thickness(1, 1, 0, 1); | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
10 changes: 10 additions & 0 deletions
10
dev/DevWinUI.Gallery/ControlPagesSampleCode/Features/ElementGroup_xaml.txt
This file contains 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,10 @@ | ||
<dev:ElementGroup> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
</dev:ElementGroup> |
This file contains 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
69 changes: 69 additions & 0 deletions
69
dev/DevWinUI.Gallery/Views/Pages/Features/ElementGroupPage.xaml
This file contains 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,69 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<Page x:Class="DevWinUIGallery.Views.ElementGroupPage" | ||
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:dev="using:DevWinUI" | ||
xmlns:local="using:DevWinUIGallery" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d"> | ||
|
||
<ScrollViewer> | ||
<StackPanel Margin="10" | ||
dev:PanelAttach.ChildrenTransitions="Default" | ||
Spacing="10"> | ||
<local:ControlExample DocPage="controls/elementGroup" | ||
XamlSource="Features/ElementGroup_xaml.txt"> | ||
<local:ControlExample.Pane> | ||
<ComboBox x:Name="CmbOrientation" | ||
Header="Orientation" | ||
SelectedIndex="0" | ||
SelectionChanged="CmbOrientation_SelectionChanged"> | ||
<ComboBoxItem Content="Horizontal" | ||
Tag="Horizontal" /> | ||
<ComboBoxItem Content="Vertical" | ||
Tag="Vertical" /> | ||
</ComboBox> | ||
</local:ControlExample.Pane> | ||
<StackPanel Spacing="10"> | ||
<dev:ElementGroup x:Name="ElementGroupSample"> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
</dev:ElementGroup> | ||
|
||
<dev:ElementGroup x:Name="ElementGroupSample2"> | ||
<Button Height="32" | ||
HorizontalAlignment="Stretch"> | ||
<SymbolIcon Symbol="Clock" /> | ||
</Button> | ||
<TextBox Height="30" | ||
HorizontalAlignment="Stretch" | ||
BorderBrush="{ThemeResource ControlElevationBorderBrush}" | ||
PlaceholderText="TextBox" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
</dev:ElementGroup> | ||
|
||
<dev:ElementGroup x:Name="ElementGroupSample3"> | ||
<TextBox Height="30" | ||
HorizontalAlignment="Stretch" | ||
BorderBrush="{ThemeResource ControlElevationBorderBrush}" | ||
PlaceholderText="TextBox" /> | ||
<Button HorizontalAlignment="Stretch" | ||
Content="Button" /> | ||
<ToggleButton HorizontalAlignment="Stretch" | ||
Content="ToggleButton" /> | ||
</dev:ElementGroup> | ||
</StackPanel> | ||
</local:ControlExample> | ||
</StackPanel> | ||
</ScrollViewer> | ||
</Page> |
21 changes: 21 additions & 0 deletions
21
dev/DevWinUI.Gallery/Views/Pages/Features/ElementGroupPage.xaml.cs
This file contains 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,21 @@ | ||
namespace DevWinUIGallery.Views; | ||
|
||
public sealed partial class ElementGroupPage : Page | ||
{ | ||
public ElementGroupPage() | ||
{ | ||
this.InitializeComponent(); | ||
} | ||
|
||
private void CmbOrientation_SelectionChanged(object sender, SelectionChangedEventArgs e) | ||
{ | ||
if (ElementGroupSample != null && ElementGroupSample2 != null && ElementGroupSample3 != null) | ||
{ | ||
var item = CmbOrientation.SelectedItem as ComboBoxItem; | ||
var orientation = GeneralHelper.GetEnum<Orientation>(item.Tag.ToString()); | ||
ElementGroupSample.Orientation = orientation; | ||
ElementGroupSample2.Orientation = orientation; | ||
ElementGroupSample3.Orientation = orientation; | ||
} | ||
} | ||
} |