private void AddRouter(String MAC, double x, double y) { var newThumb = new MyThumb(MAC); Vertex.Add(newThumb); newThumb.Template = Resources["template1"] as ControlTemplate; newThumb.ApplyTemplate(); newThumb.DragDelta += OnDragDelta; myCanvas.Children.Add(newThumb); var img = (Image)newThumb.Template.FindName("tplImage", newThumb); img.Source = new BitmapImage(new Uri("Images/router.png", UriKind.Relative)); var txt = (TextBlock)newThumb.Template.FindName("tplTextBlock", newThumb); txt.Text = MAC; Canvas.SetLeft(newThumb, x); Canvas.SetTop(newThumb, y); Panel.SetZIndex(newThumb, 1); newThumb.UpdateLayout(); Mouse.OverrideCursor = null; }
// This method updates all the starting and ending lines assigned for the given thumb // according to the latest known thumb position on the canvas private static void UpdateLines(MyThumb thumb) { var left = Canvas.GetLeft(thumb); var top = Canvas.GetTop(thumb); foreach (var line in thumb.StartLines) { line.StartPoint = new Point(left + thumb.ActualWidth / 2, top + thumb.ActualHeight / 2); } foreach (var line in thumb.EndLines) { line.EndPoint = new Point(left + thumb.ActualWidth / 2, top + thumb.ActualHeight / 2); } }
// Event handler for creating new thumb element by left mouse click // and visually connecting it to the myThumb2 element private void Window1PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (!_isAddNew) { return; } // Create new thumb object var newThumb = new MyThumb("example"); // Assign our custom template to it newThumb.Template = Resources["template1"] as ControlTemplate; // Calling ApplyTemplate enables us to navigate the visual tree right now (important!) newThumb.ApplyTemplate(); // Add the "onDragDelta" event handler that is common to all objects newThumb.DragDelta += OnDragDelta; // Put newly created thumb on the canvas myCanvas.Children.Add(newThumb); // Access the image element of our custom template and assign it to the new image var img = (Image)newThumb.Template.FindName("tplImage", newThumb); img.Source = new BitmapImage(new Uri("Images/router.png", UriKind.Relative)); // Access the textblock element of template and change it too var txt = (TextBlock)newThumb.Template.FindName("tplTextBlock", newThumb); txt.Text = "System action"; // Set the position of the object according to the mouse pointer var position = e.GetPosition(this); Canvas.SetLeft(newThumb, position.X); Canvas.SetTop(newThumb, position.Y); // Move our thumb to the front to be over the lines Panel.SetZIndex(newThumb, 1); // Manually update the layout of the thumb (important!) newThumb.UpdateLayout(); // Create new path and put it on the canvas var newPath = new Path { Stroke = Brushes.Black, StrokeThickness = 1 }; myCanvas.Children.Add(newPath); // Create new line geometry element and assign the path to it var newLine = new LineGeometry(); newPath.Data = newLine; // Predefined "myThumb2" element will host the starting point // Our new thumb will host the ending point of the line _isAddNew = false; Mouse.OverrideCursor = null; btnNewAction.IsEnabled = true; e.Handled = true; }