Skip to content

Instantly share code, notes, and snippets.

@MoaidHathot
Last active August 24, 2017 09:55
Show Gist options
  • Save MoaidHathot/b05b7b9d7cdb95af000071a67bb08e31 to your computer and use it in GitHub Desktop.
Save MoaidHathot/b05b7b9d7cdb95af000071a67bb08e31 to your computer and use it in GitHub Desktop.
OrientationStackPanel
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WPFLayoutTests
{
public class OrientationStackPanel : StackPanel
{
private IEnumerable<FrameworkElement> ContainedControls => InternalChildren.Cast<FrameworkElement>();
protected override Size ArrangeOverride(Size arrangeSize)
{
var leftPoint = new Point();
var rightPoint = Orientation.Horizontal == Orientation ? new Point(arrangeSize.Width, 0) : new Point(0, arrangeSize.Height);
ContainedControls.ForEach(element =>
{
Point location;
if (Orientation.Horizontal == Orientation ? HorizontalAlignment.Right != element.HorizontalAlignment : VerticalAlignment.Bottom != element.VerticalAlignment)
{
location = leftPoint;
leftPoint = Orientation.Horizontal == Orientation ? new Point(leftPoint.X + element.DesiredSize.Width, leftPoint.Y) : new Point(leftPoint.X, leftPoint.Y + element.DesiredSize.Height);
}
else
{
rightPoint = Orientation.Horizontal == Orientation ? new Point(rightPoint.X - element.DesiredSize.Width, rightPoint.Y) : new Point(rightPoint.X, rightPoint.Y - element.DesiredSize.Height);
location = rightPoint;
}
element.Arrange(new Rect(location, new Size(element.DesiredSize.Width, element.DesiredSize.Height)));
});
return arrangeSize;
}
}
}
@MoaidHathot
Copy link
Author

MoaidHathot commented Aug 24, 2017

Example of use:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment