Blame view

sources/RoboforkApp/Controls/DesignerItem.cs 3.25 KB
729be9a6d   doan   New Project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Input;
  using System.Windows.Media;
  
  namespace RoboforkApp
  {
      public class DesignerItem : ContentControl
      {
          public bool IsSelected
          {
              get { return (bool)GetValue(IsSelectedProperty); }
              set { SetValue(IsSelectedProperty, value); }
          }
  
          public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool),
                                        typeof(DesignerItem),
                                        new FrameworkPropertyMetadata(false));
  
          public static readonly DependencyProperty MoveThumbTemplateProperty =
              DependencyProperty.RegisterAttached("MoveThumbTemplate", typeof(ControlTemplate), typeof(DesignerItem));
  
          public static ControlTemplate GetMoveThumbTemplate(UIElement element)
          {
              return (ControlTemplate)element.GetValue(MoveThumbTemplateProperty);
          }
  
          public static void SetMoveThumbTemplate(UIElement element, ControlTemplate value)
          {
              element.SetValue(MoveThumbTemplateProperty, value);
          }
  
          static DesignerItem()
          {
              FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(DesignerItem), new FrameworkPropertyMetadata(typeof(DesignerItem)));
          }
  
          public DesignerItem()
          {
              this.Loaded += new RoutedEventHandler(this.DesignerItem_Loaded);
          }
  
          protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
          {
              base.OnPreviewMouseDown(e);
              DesignerCanvas designer = VisualTreeHelper.GetParent(this) as DesignerCanvas;
  
              if (designer != null)
              {
                  if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                  {
                      this.IsSelected = !this.IsSelected;
                  }
                  else
                  {
                      if (!this.IsSelected)
                      {
                          designer.DeselectAll();
                          this.IsSelected = true;
                      }
                  }
              }
  
              e.Handled = false;
          }
  
          private void DesignerItem_Loaded(object sender, RoutedEventArgs e)
          {
              if (this.Template != null)
              {
                  ContentPresenter contentPresenter =
                      this.Template.FindName("PART_ContentPresenter", this) as ContentPresenter;
  
                  MoveThumb thumb =
                      this.Template.FindName("PART_MoveThumb", this) as MoveThumb;
  
                  if (contentPresenter != null && thumb != null)
                  {
                      UIElement contentVisual =
                          VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;
  
                      if (contentVisual != null)
                      {
                          ControlTemplate template =
                              DesignerItem.GetMoveThumbTemplate(contentVisual) as ControlTemplate;
  
                          if (template != null)
                          {
                              thumb.Template = template;
                          }
                      }
                  }
              }
          }
      }
  }