Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Markup;
- namespace sky
- {
- /// <summary>
- /// Interaction logic for MapKnot.xaml
- /// </summary>
- [ContentProperty("ChildKnots")]
- public partial class MapKnot : StackPanel
- {
- List<FrameworkElementAdorner> connectionLines = new List<FrameworkElementAdorner>();
- int childCount = 0;
- public MapKnot()
- {
- InitializeComponent();
- LayoutUpdated += new EventHandler(MapKnot_LayoutUpdated);
- double fontSize = KnotTextContainer.FontSize;
- KnotTextContainer.Margin = new Thickness(0, fontSize/2, fontSize * 2, fontSize/2);
- }
- void MapKnot_LayoutUpdated(object sender, EventArgs e)
- {
- if (childCount != ChildKnotsContainer.Children.Count)
- {
- childCount = ChildKnotsContainer.Children.Count;
- Point rootBegin = KnotTextContainer.TranslatePoint(
- new Point(0, KnotTextContainer.ActualHeight), this);
- Point rootEnd = KnotTextContainer.TranslatePoint(
- new Point(KnotTextContainer.ActualWidth, KnotTextContainer.ActualHeight), this);
- AdornerLayer layer = AdornerLayer.GetAdornerLayer(this);
- foreach(FrameworkElementAdorner line in connectionLines)
- {
- layer.Remove(line);
- }
- FrameworkElementAdorner line1 = CreateLine(rootBegin, rootEnd);
- layer.Add(line1);
- connectionLines.Add(line1);
- foreach (FrameworkElement childKnot in ChildKnots)
- {
- TextBlock childKnotText = (childKnot as MapKnot).KnotTextContainer;
- Point childBegin = childKnotText.TranslatePoint(
- new Point(0, childKnotText.ActualHeight), this);
- Point childEnd = childKnotText.TranslatePoint(
- new Point(childKnotText.ActualWidth, childKnotText.ActualHeight), this);
- FrameworkElementAdorner line2 = CreateLine(rootEnd, childBegin);
- layer.Add(line2);
- connectionLines.Add(line2);
- FrameworkElementAdorner line3 = CreateLine(childBegin, childEnd);
- layer.Add(line3);
- connectionLines.Add(line3);
- }
- }
- }
- private FrameworkElementAdorner CreateLine(Point from, Point to)
- {
- Line line = new Line();
- line.X1 = from.X;
- line.X2 = to.X;
- line.Y1 = from.Y;
- line.Y2 = to.Y;
- line.Stroke = Brushes.Black;
- return new FrameworkElementAdorner(this, line);
- }
- public string KnotText
- {
- get { return KnotTextContainer.Text; }
- set { KnotTextContainer.Text = value; }
- }
- public UIElementCollection ChildKnots
- {
- get { return ChildKnotsContainer.Children; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement