Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PropertyViewItem.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using System;
5 using System.Collections.Generic;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Input;
9 
10 using SiliconStudio.Presentation.Collections;
11 using SiliconStudio.Presentation.Extensions;
12 
13 namespace SiliconStudio.Presentation.Controls
14 {
16  {
17  private readonly ObservableList<PropertyViewItem> properties = new ObservableList<PropertyViewItem>();
18  private readonly PropertyView propertyView;
19 
20  public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(PropertyViewItem), new FrameworkPropertyMetadata(false, OnIsExpandedChanged));
21 
22  public static readonly DependencyProperty CanBeHoveredProperty = DependencyProperty.Register("CanBeHovered", typeof(bool), typeof(PropertyViewItem), new FrameworkPropertyMetadata(true));
23 
24  public static readonly DependencyPropertyKey OffsetPropertyKey = DependencyProperty.RegisterReadOnly("Offset", typeof(double), typeof(PropertyViewItem), new FrameworkPropertyMetadata(0.0));
25 
26  public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(PropertyViewItem), new FrameworkPropertyMetadata(0.0, OnIncrementChanged));
27 
28  public static readonly RoutedEvent ExpandedEvent = EventManager.RegisterRoutedEvent("Expanded", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PropertyViewItem));
29 
30  public static readonly RoutedEvent CollapsedEvent = EventManager.RegisterRoutedEvent("Collapsed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PropertyViewItem));
31 
32  static PropertyViewItem()
33  {
34  DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyViewItem), new FrameworkPropertyMetadata(typeof(PropertyViewItem)));
35  }
36 
37  public PropertyViewItem(PropertyView propertyView)
38  {
39  if (propertyView == null) throw new ArgumentNullException("propertyView");
40  this.propertyView = propertyView;
41  PreviewMouseMove += propertyView.ItemMouseMove;
42 
43  }
44 
45  public PropertyView PropertyView { get { return propertyView; } }
46 
47  public IReadOnlyCollection<PropertyViewItem> Properties { get { return properties; } }
48 
49  public bool IsExpanded { get { return (bool)GetValue(IsExpandedProperty); } set { SetValue(IsExpandedProperty, value); } }
50 
51  public bool CanBeHovered { get { return (bool)GetValue(CanBeHoveredProperty); } set { SetValue(CanBeHoveredProperty, value); } }
52 
53  public double Offset { get { return (double)GetValue(OffsetPropertyKey.DependencyProperty); } private set { SetValue(OffsetPropertyKey, value); } }
54 
55  public double Increment { get { return (double)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } }
56 
57  public event RoutedEventHandler Expanded { add { AddHandler(ExpandedEvent, value); } remove { RemoveHandler(ExpandedEvent, value); } }
58 
59  public event RoutedEventHandler Collapsed { add { AddHandler(CollapsedEvent, value); } remove { RemoveHandler(CollapsedEvent, value); } }
60 
62  {
63  var item = new PropertyViewItem(propertyView) { Offset = Offset + Increment };
64  return item;
65  }
66 
67  protected override bool IsItemItsOwnContainerOverride(object item)
68  {
69  return item is PropertyViewItem;
70  }
71 
72  protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
73  {
74  if (!e.Handled && IsEnabled)
75  {
76  if (Focus())
77  {
78  e.Handled = true;
79  }
80  if (e.ClickCount % 2 == 0)
81  {
82  SetCurrentValue(IsExpandedProperty, !IsExpanded);
83  e.Handled = true;
84  }
85  }
86  base.OnMouseLeftButtonDown(e);
87  }
88 
89  protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
90  {
91  base.PrepareContainerForItemOverride(element, item);
92  var container = (PropertyViewItem)element;
93  properties.Add(container);
94  RaiseEvent(new PropertyViewItemEventArgs(PropertyView.PrepareItemEvent, this, container, item));
95  }
96 
97  protected override void ClearContainerForItemOverride(DependencyObject element, object item)
98  {
99  var container = (PropertyViewItem)element;
100  RaiseEvent(new PropertyViewItemEventArgs(PropertyView.ClearItemEvent, this, (PropertyViewItem)element, item));
101  properties.Remove(container);
102  base.ClearContainerForItemOverride(element, item);
103  }
104 
105  // TODO
106  //protected override AutomationPeer OnCreateAutomationPeer()
107  //{
108  // return (AutomationPeer)new TreeViewItemAutomationPeer(this);
109  //}
110 
111  protected virtual void OnExpanded(RoutedEventArgs e)
112  {
113  RaiseEvent(e);
114  }
115 
116  protected virtual void OnCollapsed(RoutedEventArgs e)
117  {
118  RaiseEvent(e);
119  }
120 
121  private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
122  {
123  var item = (PropertyViewItem)d;
124  var isExpanded = (bool)e.NewValue;
125  //ItemsPresenter itemsHostPresenter = item.ItemsHostPresenter;
126  //if (itemsHostPresenter != null)
127  //{
128  // collapsed.InvalidateMeasure();
129  // MS.Internal.Helper.InvalidateMeasureOnPath((DependencyObject)itemsHostPresenter, (DependencyObject)collapsed, false);
130  //}
131  //TreeViewItemAutomationPeer itemAutomationPeer = UIElementAutomationPeer.FromElement((UIElement)collapsed) as TreeViewItemAutomationPeer;
132  //if (itemAutomationPeer != null)
133  // itemAutomationPeer.RaiseExpandCollapseAutomationEvent((bool)e.OldValue, newValue);
134  if (isExpanded)
135  item.OnExpanded(new RoutedEventArgs(ExpandedEvent, item));
136  else
137  item.OnCollapsed(new RoutedEventArgs(CollapsedEvent, item));
138  }
139 
140  private static void OnIncrementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
141  {
142  var item = (PropertyViewItem)d;
143  var delta = (double)e.NewValue - (double)e.OldValue;
144  var subItems = item.FindVisualChildrenOfType<PropertyViewItem>();
145  foreach (var subItem in subItems)
146  {
147  subItem.Offset += delta;
148  }
149  }
150  }
151 }
static readonly RoutedEvent PrepareItemEvent
Identifies the PreparePropertyItem event. This attached routed event may be raised by the PropertyGri...
Definition: PropertyView.cs:27
override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
static readonly RoutedEvent ClearItemEvent
Identifies the ClearPropertyItem event. This attached routed event may be raised by the PropertyGrid ...
Definition: PropertyView.cs:34
override void PrepareContainerForItemOverride(DependencyObject element, object item)
override void ClearContainerForItemOverride(DependencyObject element, object item)