Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Windows.Markup;
  15.  
  16. namespace sky
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MapKnot.xaml
  20.     /// </summary>
  21.     [ContentProperty("ChildKnots")]
  22.     public partial class MapKnot : StackPanel
  23.     {
  24.         List<FrameworkElementAdorner> connectionLines = new List<FrameworkElementAdorner>();
  25.         int childCount = 0;
  26.  
  27.         public MapKnot()
  28.         {
  29.             InitializeComponent();
  30.             LayoutUpdated += new EventHandler(MapKnot_LayoutUpdated);
  31.             double fontSize = KnotTextContainer.FontSize;
  32.             KnotTextContainer.Margin = new Thickness(0, fontSize/2, fontSize * 2, fontSize/2);
  33.         }
  34.  
  35.         void MapKnot_LayoutUpdated(object sender, EventArgs e)
  36.         {
  37.             if (childCount != ChildKnotsContainer.Children.Count)
  38.             {
  39.                 childCount = ChildKnotsContainer.Children.Count;
  40.                
  41.                 Point rootBegin = KnotTextContainer.TranslatePoint(
  42.                     new Point(0, KnotTextContainer.ActualHeight), this);
  43.                 Point rootEnd = KnotTextContainer.TranslatePoint(
  44.                     new Point(KnotTextContainer.ActualWidth, KnotTextContainer.ActualHeight), this);
  45.                 AdornerLayer layer = AdornerLayer.GetAdornerLayer(this);
  46.                 foreach(FrameworkElementAdorner line in connectionLines)
  47.                 {
  48.                     layer.Remove(line);
  49.                 }
  50.  
  51.                 FrameworkElementAdorner line1 = CreateLine(rootBegin, rootEnd);
  52.                 layer.Add(line1);
  53.                 connectionLines.Add(line1);
  54.  
  55.                 foreach (FrameworkElement childKnot in ChildKnots)
  56.                 {
  57.                     TextBlock childKnotText = (childKnot as MapKnot).KnotTextContainer;
  58.                     Point childBegin = childKnotText.TranslatePoint(
  59.                         new Point(0, childKnotText.ActualHeight), this);
  60.                     Point childEnd = childKnotText.TranslatePoint(
  61.                         new Point(childKnotText.ActualWidth, childKnotText.ActualHeight), this);
  62.  
  63.                     FrameworkElementAdorner line2 = CreateLine(rootEnd, childBegin);
  64.                     layer.Add(line2);
  65.                     connectionLines.Add(line2);
  66.  
  67.                     FrameworkElementAdorner line3 = CreateLine(childBegin, childEnd);
  68.                     layer.Add(line3);
  69.                     connectionLines.Add(line3);
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private FrameworkElementAdorner CreateLine(Point from, Point to)
  75.         {
  76.             Line line = new Line();
  77.             line.X1 = from.X;
  78.             line.X2 = to.X;
  79.             line.Y1 = from.Y;
  80.             line.Y2 = to.Y;
  81.             line.Stroke = Brushes.Black;
  82.             return new FrameworkElementAdorner(this, line);
  83.         }
  84.  
  85.         public string KnotText
  86.         {
  87.             get { return KnotTextContainer.Text; }
  88.             set { KnotTextContainer.Text = value; }
  89.         }
  90.  
  91.         public UIElementCollection ChildKnots
  92.         {
  93.             get { return ChildKnotsContainer.Children; }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement