Hi,
You can find some sample code here:
http://mindfusion.eu/Forum/YaBB.pl?num=1412696051That code is for Windows Forms; replace all RectangleF with Rect and float with double to compile under WPF. Centering using the methods above will look like this:
void AlignCenterX()
{
Align(
r => r.X + r.Width / 2,
(r, coord) => new Rect(coord - r.Width / 2, r.Y, r.Width, r.Height));
}
void AlignCenterY()
{
Align(
r => r.Y + r.Height / 2,
(r, coord) => new Rect(r.X, coord - r.Height / 2, r.Width, r.Height));
}
void Align(
Func<Rect, double> getter,
Func<Rect, double, Rect> setter)
{
var target = diagram.ActiveItem as DiagramNode;
if (target != null)
{
var alignedCoord = getter(target.Bounds);
foreach (var node in diagram.Selection.Nodes)
{
if (node == target)
continue;
node.Bounds = setter(node.Bounds, alignedCoord);
}
}
}
void AlignLeft()
{
Align(
r => r.Left,
(r, coord) => new Rect(coord, r.Y, r.Width, r.Height));
}
void AlignTop()
{
Align(
r => r.Top,
(r, coord) => new Rect(r.X, coord, r.Width, r.Height));
}
void AlignRight()
{
Align(
r => r.Right,
(r, coord) => new Rect(coord - r.Width, r.Y, r.Width, r.Height));
}
void AlignBottom()
{
Align(
r => r.Bottom,
(r, coord) => new Rect(r.X, coord - r.Height, r.Width, r.Height));
}
I hope that helps,
Stoyan