Last active
August 24, 2017 09:55
-
-
Save MoaidHathot/b05b7b9d7cdb95af000071a67bb08e31 to your computer and use it in GitHub Desktop.
OrientationStackPanel
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
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of use: