c44024f4d650f85e3bd36d38a025c3d0ed775afe
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / TreeView / DesignerTreeView.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View.TreeView
6 {
7     using System.Activities.Presentation.Model;
8     using System.Collections.ObjectModel;
9     using System.Windows;
10     using System.Windows.Threading;
11
12     partial class DesignerTreeView : System.Windows.Controls.TreeView
13     {
14         private TreeViewItemModelItemViewModel rootModelItemViewModel;
15         private bool isSelectionSubscribed;
16
17         internal bool IsSelectionChangeHandledByTreeView { get; set; }
18
19         public DesignerTreeView()
20         {
21             InitializeComponent();
22         }
23
24         public EditingContext Context
25         {
26             get;
27             private set;
28         }
29
30         public void SetRootDesigner(ModelItem modelItem)
31         {
32             rootModelItemViewModel = new TreeViewItemModelItemViewModel(null, modelItem);
33             this.ItemsSource = new ObservableCollection<TreeViewItemModelItemViewModel>() { rootModelItemViewModel };
34
35             if (!isSelectionSubscribed)
36             {
37                 Selection.Subscribe(Context, ModelItemSelectionChanged);
38                 isSelectionSubscribed = true;
39             }
40         }
41
42         public void Initialize(EditingContext context)
43         {
44             this.Context = context;
45         }
46
47         private void ModelItemSelectionChanged(Selection selection)
48         {
49             // AutoExpand only when designerTreeView didn't handle the selection change in modelItem
50             if (this.IsSelectionChangeHandledByTreeView)
51             {
52                 this.IsSelectionChangeHandledByTreeView = false;
53             }
54             else
55             {
56                 if (selection.PrimarySelection != null)
57                 {
58                     TreeViewItemViewModel itemToBeSelected = DesignerTreeAutoExpandHelper.Expand(this.rootModelItemViewModel, selection.PrimarySelection);
59                     // itemToBeSelected == null means needn't AutoExpand.
60                     if (itemToBeSelected != null)
61                     {
62                         this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
63                         {
64                             // TreeViewItemViewModel may got removed before Idle.
65                             if (itemToBeSelected.IsAlive && itemToBeSelected.TreeViewItem != null)
66                             {
67                                 itemToBeSelected.TreeViewItem.Select();
68                             }
69                             // reset this flag to false, because the operation is done.
70                             // The flag will be used for next operation.
71                             this.IsSelectionChangeHandledByTreeView = false;
72                         }));
73                     }
74                 }
75             }
76         }
77
78         public void RestoreDesignerStates()
79         {
80             this.InvalidateMeasure();
81         }
82
83         protected override DependencyObject GetContainerForItemOverride()
84         {
85             return new DesignerTreeViewItem() { ParentTreeView = this };
86         }
87     }
88 }