1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View.TreeView
7 using System.Activities.Presentation.Model;
8 using System.Collections.ObjectModel;
10 using System.Windows.Threading;
12 partial class DesignerTreeView : System.Windows.Controls.TreeView
14 private TreeViewItemModelItemViewModel rootModelItemViewModel;
15 private bool isSelectionSubscribed;
17 internal bool IsSelectionChangeHandledByTreeView { get; set; }
19 public DesignerTreeView()
21 InitializeComponent();
24 public EditingContext Context
30 public void SetRootDesigner(ModelItem modelItem)
32 rootModelItemViewModel = new TreeViewItemModelItemViewModel(null, modelItem);
33 this.ItemsSource = new ObservableCollection<TreeViewItemModelItemViewModel>() { rootModelItemViewModel };
35 if (!isSelectionSubscribed)
37 Selection.Subscribe(Context, ModelItemSelectionChanged);
38 isSelectionSubscribed = true;
42 public void Initialize(EditingContext context)
44 this.Context = context;
47 private void ModelItemSelectionChanged(Selection selection)
49 // AutoExpand only when designerTreeView didn't handle the selection change in modelItem
50 if (this.IsSelectionChangeHandledByTreeView)
52 this.IsSelectionChangeHandledByTreeView = false;
56 if (selection.PrimarySelection != null)
58 TreeViewItemViewModel itemToBeSelected = DesignerTreeAutoExpandHelper.Expand(this.rootModelItemViewModel, selection.PrimarySelection);
59 // itemToBeSelected == null means needn't AutoExpand.
60 if (itemToBeSelected != null)
62 this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
64 // TreeViewItemViewModel may got removed before Idle.
65 if (itemToBeSelected.IsAlive && itemToBeSelected.TreeViewItem != null)
67 itemToBeSelected.TreeViewItem.Select();
69 // reset this flag to false, because the operation is done.
70 // The flag will be used for next operation.
71 this.IsSelectionChangeHandledByTreeView = false;
78 public void RestoreDesignerStates()
80 this.InvalidateMeasure();
83 protected override DependencyObject GetContainerForItemOverride()
85 return new DesignerTreeViewItem() { ParentTreeView = this };