1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.Activities.Presentation.FreeFormEditing
8 using System.Globalization;
10 using System.Windows.Data;
11 using System.Windows.Media;
12 using System.Diagnostics.CodeAnalysis;
14 [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "The class is used in xaml.")]
15 class ConnectorLabelMarginConverter : IMultiValueConverter
17 const double EPS = 1e-6;
19 [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "The class is only used internally and not accessible externally.")]
20 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
22 Thickness margin = new Thickness(0);
23 PointCollection connectorPoints = values[0] as PointCollection;
24 // 8 and 4 is calcuated from the margin / padding settings related to the label in xaml
25 double labelBorderWidth = (double)values[1] + 8;
26 double labelBorderHeight = (double)values[2] + 4;
27 if (connectorPoints != null)
29 int longestSegmentIndex;
30 DesignerGeometryHelper.LongestSegmentLength(connectorPoints, out longestSegmentIndex);
31 if (longestSegmentIndex >= 0)
33 Point labelLocation = DesignerGeometryHelper.MidPointOfLineSegment(connectorPoints[longestSegmentIndex], connectorPoints[longestSegmentIndex + 1]);
34 labelLocation.X = (int)(labelLocation.X - labelBorderWidth / 2 + EPS);
35 labelLocation.Y = (int)(labelLocation.Y - labelBorderHeight / 2 + EPS);
36 margin.Top = labelLocation.Y;
37 margin.Left = labelLocation.X;
43 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
45 throw FxTrace.Exception.AsError(new NotSupportedException());