Beispiel #1
0
        internal void ChangeSelection(object data, CustomTreeViewItem container, bool?selected)
        {
            if (SelectMode == SelectionMode.Single)
            {
                if (selected.Value)
                {//单选时选中
                    _selectedContainers.ForEach(r =>
                    {
                        r.IsSelected = false;
                    });
                    _selectedContainers.Clear();
                    container.IsSelected = selected.Value;
                    _selectedContainers.Add(container);
                    SetValue(SelectedItemsPropertyKey, new List <object>()
                    {
                        data
                    });
                }
            }
            else if (SelectMode != SelectionMode.Single)
            {
                container.IsSelected = selected;
                if (selected.HasValue)
                {
                    if (selected.Value)
                    {//多选选中
                        if (_selectedContainers.Contains(container) == false)
                        {
                            _selectedContainers.Add(container);
                        }
                        var selItems = SelectedItems;
                        if (selItems.Contains(data) == false)
                        {
                            selItems.Add(data);
                            SetValue(SelectedItemsPropertyKey, selItems);
                        }
                    }
                    else
                    {
                        if (_selectedContainers.Contains(container))
                        {
                            _selectedContainers.Remove(container);
                        }
                        var selItems = SelectedItems;
                        if (selItems.Contains(data))
                        {
                            selItems.Remove(data);
                            SetValue(SelectedItemsPropertyKey, selItems);
                        }
                    }
                }
            }
            RoutedEventArgs args = new RoutedEventArgs(SelectedItemsChangedEvent, this);

            RaiseEvent(args);
        }
Beispiel #2
0
        private static void OnExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CustomTreeViewItem treeViewItem = d as CustomTreeViewItem;

            if ((bool)e.NewValue)
            {
                RoutedEventArgs args = new RoutedEventArgs(ExpandedEvent, treeViewItem);
                treeViewItem.RaiseEvent(args);
            }
            else
            {
                RoutedEventArgs args = new RoutedEventArgs(CollapsedEvent, treeViewItem);
                treeViewItem.RaiseEvent(args);
            }
        }
Beispiel #3
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            CustomTreeViewItem item = (CustomTreeViewItem)value;
            ItemsControl       ic   = ItemsControl.ItemsControlFromItemContainer(item);

            if (ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1)
            {
                return("l");
            }
            else if (ic.ItemContainerGenerator.IndexFromContainer(item) == 0)
            {
                return("f");
            }
            else
            {
                return("");
            }
        }
Beispiel #4
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);
            }
        }
Beispiel #5
0
        private void CascadingSetParentIsSelected(CustomTreeViewItem parent)
        {
            if (parent == null)
            {
                return;
            }
            int selectedCnt   = 0;
            int unselectedCnt = 0;

            for (int i = 0; i < parent.Items.Count; i++)
            {
                CustomTreeViewItem item = parent.ItemContainerGenerator.ContainerFromIndex(i) as CustomTreeViewItem;
                if (item.IsSelected.HasValue)
                {
                    if (item.IsSelected.Value)
                    {
                        selectedCnt += 1;
                    }
                    else
                    {
                        unselectedCnt += 1;
                    }
                }
            }
            if (selectedCnt == parent.Items.Count)
            {
                parent.IsSelected = true;
            }
            else if (unselectedCnt == parent.Items.Count)
            {
                parent.IsSelected = false;
            }
            else
            {
                parent.IsSelected = null;
            }
            CascadingSetParentIsSelected(ItemsControl.ItemsControlFromItemContainer(parent) as CustomTreeViewItem);
        }