Skip to content

Commit 45a290c

Browse files
committed
add horizontal alignment support
1 parent 471504b commit 45a290c

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/WrapPanel/WrapPanel.bind

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
Padding="@[Padding:Thickness:0,0,0,0]@"
4848
VerticalSpacing="@[VerticalSpacing:Slider:5:0-200]@"
4949
HorizontalSpacing="@[HorizontalSpacing:Slider:5:0-200]@"
50-
VerticalContentAlignment="@[VerticalContentAlignment:Enum:VerticalAlignment.Top]"/>
50+
VerticalContentAlignment="@[VerticalContentAlignment:Enum:VerticalAlignment.Top]@"
51+
HorizontalContentAlignment="@[HorizontalContentAlignment:Enum:HorizontalAlignment.Left]@" />
5152
</ItemsPanelTemplate>
5253
</ItemsControl.ItemsPanel>
5354
<ListView.ItemContainerStyle>
5455
<Style TargetType="ListViewItem">
5556
<Setter Property="VerticalContentAlignment" Value="Stretch" />
5657
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
58+
<Setter Property="Padding" Value="0" />
5759
</Style>
5860
</ListView.ItemContainerStyle>
5961
</ListView>

Microsoft.Toolkit.Uwp.UI.Controls/WrapPanel/WrapPanel.Data.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ public UvRect WithVerticalAlignment(VerticalAlignment alignment, double maxHeigh
8585
}
8686
}
8787

88+
public UvRect WithHorizontalAlignment(HorizontalAlignment alignment, double maxHeight)
89+
{
90+
switch (alignment)
91+
{
92+
case HorizontalAlignment.Center:
93+
return new UvRect
94+
{
95+
Position = Position.Add(
96+
u: 0,
97+
v: Math.Max((maxHeight - Size.V) / 2.0, 0.0)),
98+
Size = Size,
99+
};
100+
case HorizontalAlignment.Right:
101+
return new UvRect
102+
{
103+
Position = Position.Add(
104+
u: 0,
105+
v: Math.Max(maxHeight - Size.V, 0.0)),
106+
Size = Size,
107+
};
108+
case HorizontalAlignment.Stretch:
109+
return new UvRect
110+
{
111+
Position = Position,
112+
Size = new UvMeasure { U = Size.U, V = maxHeight },
113+
};
114+
case HorizontalAlignment.Left:
115+
default:
116+
return this;
117+
}
118+
}
119+
88120
public Rect ToRect(Orientation orientation)
89121
{
90122
switch (orientation)

Microsoft.Toolkit.Uwp.UI.Controls/WrapPanel/WrapPanel.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ public VerticalAlignment VerticalContentAlignment
7373
typeof(WrapPanel),
7474
new PropertyMetadata(VerticalAlignment.Top, LayoutPropertyChanged));
7575

76+
/// <summary>
77+
/// Gets or sets the horizontal alignment to use when the control <see cref="Orientation"/> is set to <see cref="Orientation.Vertical"/>.
78+
/// </summary>
79+
public HorizontalAlignment HorizontalContentAlignment
80+
{
81+
get => (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty);
82+
set => SetValue(HorizontalContentAlignmentProperty, value);
83+
}
84+
85+
/// <summary>
86+
/// The DP to store the <see cref="HorizontalContentAlignment"/> property value.
87+
/// </summary>
88+
public static readonly DependencyProperty HorizontalContentAlignmentProperty = DependencyProperty.Register(
89+
nameof(HorizontalContentAlignment),
90+
typeof(HorizontalAlignment),
91+
typeof(WrapPanel),
92+
new PropertyMetadata(HorizontalAlignment.Left, LayoutPropertyChanged));
93+
7694
/// <summary>
7795
/// Gets or sets the orientation of the WrapPanel.
7896
/// Horizontal means that child controls will be added horizontally until the width of the panel is reached, then a new row is added to add new child controls.
@@ -287,10 +305,11 @@ void Arrange(UIElement child, bool isLast = false)
287305
}
288306
else
289307
{
290-
arrangeRect = rect;
308+
arrangeRect = rect.WithHorizontalAlignment(HorizontalContentAlignment, row.Size.V);
291309
}
292310

293-
child.Arrange(arrangeRect.ToRect(Orientation));
311+
var finalRect = arrangeRect.ToRect(Orientation);
312+
child.Arrange(finalRect);
294313
}
295314
}
296315
}

0 commit comments

Comments
 (0)