1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.Activities.Presentation.Toolbox
8 using System.Windows.Automation;
9 using System.Windows.Controls;
10 using System.Windows.Data;
11 using System.Windows.Input;
13 using System.Globalization;
15 // This class is used to provide tree view item container template for Category
18 sealed class TreeViewContainerStyleSelector : StyleSelector
21 Style styleForToolboxItem;
22 Style styleForCategoryItem;
24 public TreeViewContainerStyleSelector(ToolboxControl owner)
29 //default style for ToolboxItemWrapper - this one is required to enable context search in tree view control
30 Style GetStyleForToolboxItem(Style baseStyle)
32 if (null == this.styleForToolboxItem)
34 this.styleForToolboxItem =
35 (null == baseStyle ? new Style(typeof(TreeViewItem)) : new Style(typeof(TreeViewItem), baseStyle));
37 //visibility information - i need to bind TreeViewItem to it
38 if (null != this.owner.searchBox)
40 MultiBinding visibilityBinding = new MultiBinding() { Converter = new ToolItemVisibilityConverter() };
41 Binding searchTextBinding = new Binding();
42 searchTextBinding.Source = this.owner.searchBox;
43 searchTextBinding.Path = new PropertyPath(TextBox.TextProperty);
44 searchTextBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
45 searchTextBinding.Mode = BindingMode.OneWay;
47 Binding toolItemBinding = new Binding();
48 toolItemBinding.RelativeSource = RelativeSource.Self;
49 toolItemBinding.Path = new PropertyPath(TreeViewItem.HeaderProperty);
50 toolItemBinding.Mode = BindingMode.OneWay;
52 visibilityBinding.Bindings.Add(searchTextBinding);
53 visibilityBinding.Bindings.Add(toolItemBinding);
55 Setter visibilitySetter = new Setter();
56 visibilitySetter.Property = TreeViewItem.VisibilityProperty;
57 visibilitySetter.Value = visibilityBinding;
59 //adding visibility setter to the style
60 this.styleForToolboxItem.Setters.Add(visibilitySetter);
63 //take care of the AutomationId
64 Binding automationIdBinding = new Binding();
65 automationIdBinding.RelativeSource = RelativeSource.Self;
66 automationIdBinding.Path = new PropertyPath("Header.ToolName");
67 automationIdBinding.Mode = BindingMode.OneWay;
69 Setter automationIdSetter = new Setter();
70 automationIdSetter.Property = AutomationProperties.AutomationIdProperty;
71 automationIdSetter.Value = automationIdBinding;
72 this.styleForToolboxItem.Setters.Add(automationIdSetter);
74 //to enable smooth keyboard operation, hidden items have to be disabled in order not to receive
75 //keyboard events - Trigger does the job
76 Setter enableSetter = new Setter(TreeViewItem.IsEnabledProperty, false);
78 Trigger enableTrigger = new Trigger();
79 enableTrigger.Property = TreeViewItem.VisibilityProperty;
80 enableTrigger.Value = Visibility.Collapsed;
81 enableTrigger.Setters.Add(enableSetter);
83 //to enable drag&drop support add event setter for
84 EventSetter mouseMoveSetter = new EventSetter();
85 mouseMoveSetter.Event = TreeViewItem.MouseMoveEvent;
86 mouseMoveSetter.Handler = new MouseEventHandler(this.owner.OnToolMouseMove);
87 this.styleForToolboxItem.Setters.Add(mouseMoveSetter);
89 EventSetter mouseDoubleClickSetter = new EventSetter();
90 mouseDoubleClickSetter.Event = TreeViewItem.MouseDoubleClickEvent;
91 mouseDoubleClickSetter.Handler = new MouseButtonEventHandler(this.owner.OnTreeViewDoubleClick);
92 this.styleForToolboxItem.Setters.Add(mouseDoubleClickSetter);
94 //adding trigger to the style
95 this.styleForToolboxItem.Triggers.Add(enableTrigger);
97 return this.styleForToolboxItem;
100 //default style for CategoryItem
101 Style GetStyleForCategoryItem(Style baseStyle)
103 if (null == this.styleForCategoryItem)
105 this.styleForCategoryItem =
106 (null == baseStyle ? new Style(typeof(TreeViewItem)) : new Style(typeof(TreeViewItem), baseStyle));
108 //take care of the AutomationId
109 Binding automationIdBinding = new Binding();
110 automationIdBinding.RelativeSource = RelativeSource.Self;
111 automationIdBinding.Path = new PropertyPath("Header.CategoryName");
112 automationIdBinding.Mode = BindingMode.OneWay;
114 Setter automationIdSetter = new Setter();
115 automationIdSetter.Property = AutomationProperties.AutomationIdProperty;
116 automationIdSetter.Value = automationIdBinding;
117 this.styleForCategoryItem.Setters.Add(automationIdSetter);
119 return this.styleForCategoryItem;
122 public override Style SelectStyle(object item, DependencyObject container)
124 //try get default style
125 Style result = base.SelectStyle(item, container);
126 if (item is ToolboxItemWrapper)
128 result = GetStyleForToolboxItem(this.owner.ToolItemStyle);
130 if (item is ToolboxCategory && null != this.owner.CategoryItemStyle)
132 result = GetStyleForCategoryItem( this.owner.CategoryItemStyle );
137 //helper converter - used to show/hide tool items based on the search criteria
138 sealed class ToolItemVisibilityConverter : IMultiValueConverter
140 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
142 //get the search text string
143 var text = values[0] as string;
145 var tool = values[1] as ToolboxItemWrapper;
146 var result = Visibility.Collapsed;
147 //if tool is set and is valid
148 if (null != tool && tool.IsValid)
150 //if search text is empty - show item
151 if (string.IsNullOrEmpty(text))
153 result = Visibility.Visible;
157 if (string.IsNullOrEmpty(tool.DisplayName) || tool.DisplayName.IndexOf(text, StringComparison.OrdinalIgnoreCase) < 0)
159 result = Visibility.Collapsed;
163 result = Visibility.Visible;
171 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
173 throw FxTrace.Exception.AsError(new NotSupportedException());