public static class ColumnSharedSizeHelper
// Copy From https://github.com/Qiu233/WinUISharedSizeGroup
public static readonly DependencyProperty IsSharedSizeScopeProperty =
DependencyProperty.RegisterAttached("IsSharedSizeScope", typeof(bool), typeof(UIElement), new PropertyMetadata(false));
private static readonly DependencyProperty SharedSizeGroupProperty =
DependencyProperty.RegisterAttached("SharedSizeGroup", typeof(string), typeof(UIElement), new PropertyMetadata(null));
public static void SetIsSharedSizeScope(DependencyObject o, bool group) => o.SetValue(IsSharedSizeScopeProperty, group);
public static bool GetIsSharedSizeScope(DependencyObject o) => (bool) o.GetValue(IsSharedSizeScopeProperty);
public static void SetSharedSizeGroup(DependencyObject o, string group)
o.SetValue(SharedSizeGroupProperty, group);
if (o is FrameworkElement framework)
framework.Loaded -= FrameworkOnLoaded;
framework.Loaded += FrameworkOnLoaded;
void FrameworkOnLoaded(object sender, RoutedEventArgs e)
framework.SizeChanged -= Framework_SizeChanged;
framework.SizeChanged += Framework_SizeChanged;
private static void Framework_SizeChanged(object sender, SizeChangedEventArgs args)
if (sender is not FrameworkElement currentFrameworkElement)
TrySetSize(currentFrameworkElement);
private static void TrySetSize(FrameworkElement currentFrameworkElement)
var sharedSizeGroup = GetSharedSizeGroup(currentFrameworkElement);
if (string.IsNullOrEmpty(sharedSizeGroup))
if (currentFrameworkElement.Parent is not Grid grid)
throw new InvalidOperationException();
FrameworkElement p = currentFrameworkElement;
while (!ColumnSharedSizeHelper.GetIsSharedSizeScope(p))
if (VisualTreeHelper.GetParent(p) is not FrameworkElement fe)
if (p == currentFrameworkElement)
if (!ColumnSharedSizeHelper.GetIsSharedSizeScope(p))
var group = p.GetValue(GroupsProperty) as Dictionary<string, ColumnSharedSizeGroup>;
group = new Dictionary<string, ColumnSharedSizeGroup>();
p.SetValue(GroupsProperty, group);
if (!group.TryGetValue(sharedSizeGroup, out var columnSharedSizeGroup))
columnSharedSizeGroup = new ColumnSharedSizeGroup();
group.Add(sharedSizeGroup, columnSharedSizeGroup);
columnSharedSizeGroup.Update(currentFrameworkElement);
public static string GetSharedSizeGroup(DependencyObject o)
return (string) o.GetValue(SharedSizeGroupProperty);
public static readonly DependencyProperty GroupsProperty =
DependencyProperty.RegisterAttached(nameof(ColumnSharedSizeGroup), typeof(Dictionary<string, ColumnSharedSizeGroup>), typeof(UIElement),
new PropertyMetadata(null));
class ColumnSharedSizeGroup
public void Update(FrameworkElement currentFrameworkElement)
var grid = (Grid) currentFrameworkElement.Parent;
var value = (int) currentFrameworkElement.GetValue(Grid.ColumnProperty);
var column = grid.ColumnDefinitions[value];
if (!_columns.Contains(column))
var adjustments = new List<ColumnDefinition>();
var width = currentFrameworkElement.ActualWidth + currentFrameworkElement.Margin.Left + currentFrameworkElement.Margin.Right;
adjustments.AddRange(_columns);
foreach (var columnDefinition in adjustments)
columnDefinition.Width = new GridLength(_columnSize);
private readonly List<ColumnDefinition> _columns = [];
private double _columnSize = 0.0;