public static bool WalkTreeViewItem(TreeView tree, TreeViewItem treeViewItem, object selectedValue)
        {
            if (treeViewItem.DataContext == selectedValue)
            {
                treeViewItem.SetValue(TreeViewItem.IsSelectedProperty, true);
                treeViewItem.Focus();
                treeViewItem.BringIntoView();
                return true;
            }
            var itemsHostProperty = treeViewItem.GetType().GetProperty("ItemsHost", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            var itemsHost = itemsHostProperty?.GetValue(treeViewItem, null) as Panel;

            if (itemsHost == null)
            {
                return false;
            }

            foreach (var item in itemsHost.Children.OfType<TreeViewItem>())
            {
                var oldExpanded = item.IsExpanded;
                item.IsExpanded = true;
                item.UpdateLayout();
                if (WalkTreeViewItem(tree, item, selectedValue))
                {
                    return true;
                }
                item.IsExpanded = oldExpanded;
            }

            return false;
        }
        /// <summary>
        /// Gets the value of the ConnectingLineInfo attached property for a
        /// specified TreeViewItem.
        /// </summary>
        /// <param name="element">
        /// The TreeViewItem from which the property value is read.
        /// </param>
        /// <returns>
        /// The ConnectingLineInfo property value for the TreeViewItem.
        /// </returns>
        internal static TreeViewItemConnectingLineInfo GetConnectingLineInfo(TreeViewItem element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            
            // Get the info and create on demand if necessary
            TreeViewItemConnectingLineInfo info = element.GetValue(ConnectingLineInfoProperty) as TreeViewItemConnectingLineInfo;
            if (info == null)
            {
                info = new TreeViewItemConnectingLineInfo(element);
                element.SetValue(ConnectingLineInfoProperty, info);
            }

            return info;
        }
 public static void SetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem, bool value)
 {
     treeViewItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
 }
 public static void SetIsTreeViewItemDropOver(TreeViewItem item, bool value)
 {
     item.SetValue(IsTreeViewItemDropOverProperty, value);
 }
        public static void SetIsItemSelected(TreeViewItem element, Boolean value)
        {
            if (element == null) return;

            element.SetValue(IsItemSelectedProperty, value);
        }
 public static void SetBringSelectionIntoView(TreeViewItem treeViewItem, bool value)
 {
     treeViewItem.SetValue(BringSelectionIntoViewProperty, value);
 }
 public static void SetIsFiltered(TreeViewItem item, bool value)
 {
     item.SetValue(IsFilteredProperty, value);
 }
 public static void SetEnableContextMenuWorkaround(
     TreeViewItem treeViewItem, bool value) {
     treeViewItem.SetValue(EnableContextMenuWorkaroundProperty, value);
 }
 public static void SetEnableFullRowSelection(
     TreeViewItem treeViewItem, bool value) {
     treeViewItem.SetValue(EnableFullRowSelectionProperty, value);
 }
 public static void SetBringIntoViewWhenSelected(TreeViewItem treeViewItem, bool value)
 {
     treeViewItem.SetValue(BringIntoViewWhenSelectedProperty, value);
 }
        static void SetIsChecked(TreeViewItem item, bool? value, bool updateChildren, bool updateParent)
        {
            if (value == GetIsSelected(item))
                return;

            item.SetValue(IsSelectedProperty, value);

            if (updateChildren && value.HasValue)
            {
                foreach (var treeViewItem in GetChildren(item))
                {
                    SetIsChecked(treeViewItem,value, true, false);
                }
            }

            if (updateParent && GetParentTreeViewItem(item)!= null)
                VerifyCheckState(GetParentTreeViewItem(item));
        }
 /// <summary>
 /// Sets the value of the AssociatedToolbarItem attached property to a specified TreeViewItem.
 /// </summary>
 /// <param name="element">The TreeViewItem to which the attached property is written.</param>
 /// <param name="value">The needed AssociatedToolbarItem value.</param>
 internal static void SetAssociatedToolbarItem(TreeViewItem element, AddToolbarItem value)
 {
     if (element == null)
     {
         throw new ArgumentNullException("element");
     }
     element.SetValue(AssociatedToolbarItemProperty, value);
 }
        void resource_GetChildResourcesCompleted(object sender, GetChildResourcesCompletedEventArgs e)
        {
            TreeViewItem tvi = null;
            string childName = "";

            object[] userState = e.UserState as object[];

            IDataSourceWithResources dataSource = userState[1] as IDataSourceWithResources;
            if (dataSource == null)
                return;

            // If the element at [0] is a tree view item, then this is a standard expand action on a parent node and
            // the logic flow is as it was before: remove current contents, add from array
            tvi = userState[0] as TreeViewItem;
            if (tvi != null)
            {
                tvi.Items.Clear();
            }
            else
            {
                // If the item we are adding to is the root (TreeView) then there are no children, but we are instead using the
                // progress indicator and must clear this here.
                showHideProgressIndicator(true);
                enableDisableUrlEntryUI(false);

                // Fabricate parent node and include connection, etc. so that when this node is expanded or child nodes are
                // referenced, the infrastructure will be in place to make existing logic work to obtain child information
                Resource parentResource = userState[2] as Resource;
                tvi = new TreeViewItem
                {
                    HeaderTemplate = LayoutRoot != null ? LayoutRoot.Resources["ResourceNodeDataTemplate"] as DataTemplate : null,
                    Header = parentResource,
                    DataContext = parentResource,
                    IsExpanded = true,
                    IsSelected = true,
                    Style = LayoutRoot != null ? LayoutRoot.Resources["TreeViewItemStyle"] as Style : null,
                };
                Connection connection = userState[3] as Connection;
                tvi.SetValue(TreeViewItemExtensions.ConnectionProperty, connection);
                treeResources.Items.Add(tvi);

                // If the parent item "tag" is assigned a value, store it for comparison against all child elements so that
                // the proper child element can be automatically selected. This is used when the URL contains the child
                // element of a map service, gp service, feature service, etc.
                if (parentResource.Tag != null)
                {
                    childName = parentResource.Tag as string;
                }
            }

            if (e.ChildResources == null)
                return;

            foreach (Resource childResource in e.ChildResources)
            {
                TreeViewItem treeViewItem = new TreeViewItem
                {
                    HeaderTemplate = LayoutRoot != null ? LayoutRoot.Resources["ResourceNodeDataTemplate"] as DataTemplate : null,
                    Header = childResource,
                    DataContext = childResource,
                    IsExpanded = false,
                    Style = LayoutRoot != null ? LayoutRoot.Resources["TreeViewItemStyle"] as Style : null,
                };
                if (childResource.ResourceType == ResourceType.Layer || childResource.ResourceType == ResourceType.EditableLayer)
                {
                    treeViewItem.SetValue(ToolTipService.ToolTipProperty, childResource.ResourceType == ResourceType.Layer ? Strings.MapLayer : Strings.FeatureLayer);
                }
                if (!string.IsNullOrWhiteSpace(childResource.DisplayName))
                {
                    string parentAutomationId = tvi.GetValue(System.Windows.Automation.AutomationProperties.AutomationIdProperty) as string;
                    if (parentAutomationId == null)
                        parentAutomationId = string.Empty;
                    treeViewItem.SetValue(System.Windows.Automation.AutomationProperties.AutomationIdProperty, parentAutomationId + childResource.DisplayName + childResource.ResourceType);
                }

                bool hasChildResource = dataSource.SupportsChildResources(childResource, filter);
                if (hasChildResource)
                {
                    treeViewItem.Items.Add(createRetrievingNode());
                    treeViewItem.Expanded += new RoutedEventHandler(resourceNode_Expanded);
                }

                // If a child Id was successfully extracted from the parent resource, this means there should be a child
                // resource with the same id and we need to select this as the initial URL indicated the desire to use
                // this child resource.
                if (!String.IsNullOrEmpty(childName))
                {
                    if (childResource.ResourceType == ResourceType.GPTool || childResource.ResourceType == ResourceType.DatabaseTable)
                    {
                        if (String.Compare(childName, childResource.DisplayName) == 0)
                            treeViewItem.IsSelected = true;
                    }
                    else if (childResource.ResourceType == ResourceType.Layer)
                    {
                        if (childResource.Tag != null)
                        {
                            int nodeId = (int)childResource.Tag;
                            string nodeName = String.Format("{0}", nodeId);
                            if (String.Compare(childName, nodeName) == 0)
                                treeViewItem.IsSelected = true;
                        }
                    }
                }

                tvi.Items.Add(treeViewItem);
            }

            if (tvi.Items.Count == 0)
            {
                TreeViewItem treeViewItem = CreateTreeNodeWhenNoItems();
                tvi.Items.Add(treeViewItem);
            }

            OnGetChildResourcesCompleted(e);
        }
        void dataSource_GetCatalogCompleted(object sender, GetCatalogCompletedEventArgs e)
        {
            enableDisableUrlEntryUI(false);

            object[] userState = e.UserState as object[];
            if (userState == null || userState.Length < 2)
                return;

            Connection connection = userState[0] as Connection;
            if (connection == null)
                return;

            if (e.ChildResources == null)
                return;

            if (treeResources == null)
                return;

            ESRI.ArcGIS.Mapping.Core.DataSources.DataSource ds = DataSourceProvider.CreateNewDataSourceForConnectionType(connection.ConnectionType);
            if (ds == null)
                return;

            IDataSourceWithResources dsWithResource = ds as IDataSourceWithResources;

            treeResources.Items.Clear();

            foreach (Resource childResource in e.ChildResources)
            {
                TreeViewItem treeViewItem = new TreeViewItem
                {
                    HeaderTemplate = LayoutRoot != null ? LayoutRoot.Resources["ResourceNodeDataTemplate"] as DataTemplate : null,
                    Header = childResource,
                    DataContext = childResource,
                    IsExpanded = false,
                    Style = LayoutRoot != null ? LayoutRoot.Resources["TreeViewItemStyle"] as Style : null,
                };
                if(!string.IsNullOrWhiteSpace(childResource.DisplayName))
                    treeViewItem.SetValue(System.Windows.Automation.AutomationProperties.AutomationIdProperty, childResource.DisplayName);
                
                treeViewItem.SetValue(TreeViewItemExtensions.ConnectionProperty, connection);
                if (dsWithResource != null && dsWithResource.SupportsChildResources(childResource, filter))
                {
                    treeViewItem.Items.Add(createRetrievingNode());
                    treeViewItem.Expanded += new RoutedEventHandler(resourceNode_Expanded);
                }
                treeResources.Items.Add(treeViewItem);
            }

            showHideProgressIndicator(true);
            bool addConnection = true;
            if (treeResources.Items.Count == 0)
            {
                if(!string.IsNullOrEmpty(e.Error))
                    MessageBoxDialog.Show(e.Error, ESRI.ArcGIS.Mapping.Controls.Resources.Strings.BrowseDialogGeographicDataNotFound, MessageBoxButton.OK);
                else if ((Filter & Filter.GeoprocessingServices) == Filter.GeoprocessingServices)
                    MessageBoxDialog.Show(string.Format(LocalizableStrings.NoGPServicesFound, connection.Name));
                else
                    MessageBoxDialog.Show(string.Format(LocalizableStrings.NoMapServicesFound, connection.Name));
                addConnection = false;
            }

            OnGetCatalogCompleted(e);
            bool isNewConnection = (bool)userState[1];
            if (isNewConnection)
            {
                if (addConnection)
                {
                    Connections.Add(connection);
                    if (ConnectionsProvider != null)
                        ConnectionsProvider.AddConnection(connection); // inform the provider about the new connection
                    OnConnectionAdded(new ConnectionAddedEventArgs() { Connection = connection });
                }
            }
        }