Ejemplo n.º 1
0
        /// <summary>
        /// 获取当前CustomTreeViewItem所属的CustomTreeView对象
        /// </summary>
        /// <returns></returns>
        internal CustomTreeView GetParentTreeView()
        {
            ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(this);

            while (parent != null)
            {
                CustomTreeView tv = parent as CustomTreeView;
                if (tv != null)
                {
                    return(tv);
                }

                parent = ItemsControl.ItemsControlFromItemContainer(parent);
            }

            return(null);
        }
Ejemplo n.º 2
0
        internal void SetIsSelect(CustomTreeViewItem treeViewItem, bool?isSelected)
        {
            if (treeViewItem == null)
            {
                return;
            }
            CustomTreeView tree   = GetParentTreeView();
            ItemsControl   parent = ItemsControl.ItemsControlFromItemContainer(treeViewItem);

            if (tree != null && parent != null)
            {
                object data = parent.ItemContainerGenerator.ItemFromContainer(treeViewItem);
                tree.ChangeSelection(data, treeViewItem, isSelected);
                if (IsSelected.HasValue)
                {
                    if (IsSelected.Value)
                    {
                        RoutedEventArgs args = new RoutedEventArgs(SelectedEvent, treeViewItem);
                        treeViewItem.RaiseEvent(args);
                    }
                    else
                    {
                        RoutedEventArgs args = new RoutedEventArgs(UnselectedEvent, treeViewItem);
                        treeViewItem.RaiseEvent(args);
                    }
                }
            }
            if (tree.SelectMode != SelectionMode.Single)
            {
                treeViewItem.IsExpanded = true;
                treeViewItem.UpdateLayout();
                for (int i = 0; i < treeViewItem.Items.Count; i++)
                {
                    CustomTreeViewItem subItem = (CustomTreeViewItem)treeViewItem.ItemContainerGenerator.ContainerFromIndex(i);
                    SetIsSelect(subItem, isSelected);
                }

                CascadingSetParentIsSelected(parent as CustomTreeViewItem);
            }
        }
Ejemplo n.º 3
0
 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
 {
     if (!e.Handled && IsEnabled)
     {
         if (Focus())
         {
             CustomTreeView treeview = GetParentTreeView();
             if (treeview.SelectMode != SelectionMode.Single)
             {
                 SetIsSelect(this, IsSelected == null ? true : !IsSelected);
             }
             else
             {
                 if (IsSelected == null || IsSelected.Value == false)
                 {
                     SetIsSelect(this, true);
                 }
             }
             e.Handled = true;
         }
     }
     base.OnMouseLeftButtonDown(e);
 }