コード例 #1
0
        public static bool GetItemSelected(this ItemsControl itemsControl, object item)
        {
            ItemsControlInformation info = FindItemsControlInformation(itemsControl);

            if (null != info)
            {
                return(info.GetIsItemSelected(itemsControl, item));
            }
            else if (itemsControl is MultiSelector)
            {
                return(((MultiSelector)itemsControl).SelectedItems.Contains(item));
            }
            else if (itemsControl is ListBox)
            {
                return(((ListBox)itemsControl).SelectedItems.Contains(item));
            }
            else if (itemsControl is TreeView)
            {
                return(((TreeView)itemsControl).SelectedItem == item);
            }
            else if (itemsControl is Selector)
            {
                return(((Selector)itemsControl).SelectedItem == item);
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Registers the items control.
        /// </summary>
        /// <param name="itemsControlType">Type of the items control.</param>
        /// <param name="itemsContainerType">Type of the items container the items control is using for its items.</param>
        /// <param name="CanSelectMultipleItems">Function that determines if multiple items can be selected in the items control. For some items controls, that depends on the selection mode. If the return value is <c>true</c>, multiple items can be selected.</param>
        /// <param name="GetSelectedItems">Function that retrieves the currently selected items from the items control. The argument is the items control.</param>
        /// <param name="GetIsItemSelected">Function that determines whether the provided item is selected in the items control. 1st arg is the items control, 2nd arg is the item. The return value is <c>true</c> if the item is selected in the items control.</param>
        /// <param name="SetIsItemSelected">Action that sets the selection status of an item. 1st arg is the items control, 2nd arg is the item, and 3rd arg is the selection status. If the selection status is <c>true</c>, the item must be selected in the items control. If the selection status is <c>false</c>, the item must be deselected in the items control. If the items control provides no means to set the selected item(s) programmatically, set this argument to <c>null</c>.</param>
        /// <param name="GetOrientation">Function that gets the orientation of the items with respect to each other. Lists items are usually oriented vertically, but this can be changed. Because TreeViewItems have no specific orientation with respect to each other, the return value should be <c>null</c> in this case.</param>
        /// <exception cref="System.ArgumentException">The provided items control type is not derived from ItemsControl.</exception>
        public static void RegisterItemsControl(
            System.Type itemsControlType,
            System.Type itemsContainerType,
            Func <ItemsControl, bool> CanSelectMultipleItems,
            Func <ItemsControl, IEnumerable> GetSelectedItems,
            Func <ItemsControl, object, bool> GetIsItemSelected,
            Action <ItemsControl, object, bool> SetIsItemSelected,
            Func <ItemsControl, System.Windows.Controls.Orientation?> GetOrientation)
        {
            if (!typeof(ItemsControl).IsAssignableFrom(itemsControlType))
            {
                throw new ArgumentException("The provided items control type is not derived from ItemsControl.");
            }

            var info = new ItemsControlInformation
            {
                ItemsControlType       = itemsControlType,
                ItemContainerType      = itemsContainerType,
                CanSelectMultipleItems = CanSelectMultipleItems,
                GetSelectedItems       = GetSelectedItems,
                GetIsItemSelected      = GetIsItemSelected,
                SetIsItemSelected      = SetIsItemSelected,
                GetOrientation         = GetOrientation
            };

            if (!_itemsControlRegistrationDict.ContainsKey(itemsControlType))
            {
                _itemsControlRegistrationDict.Add(itemsControlType, info);
            }
            else
            {
                _itemsControlRegistrationDict[itemsControlType] = info;
            }
        }
コード例 #3
0
        public static IEnumerable GetSelectedItems(this ItemsControl itemsControl)
        {
            ItemsControlInformation info = FindItemsControlInformation(itemsControl);

            if (null != info)
            {
                return(info.GetSelectedItems(itemsControl));
            }

            //if (itemsControl.GetType().IsAssignableFrom(typeof(MultiSelector)))
            else if (typeof(MultiSelector).IsAssignableFrom(itemsControl.GetType()))
            {
                return(((MultiSelector)itemsControl).SelectedItems);
            }
            else if (itemsControl is ListBox)
            {
                var listBox = (ListBox)itemsControl;

                if (listBox.SelectionMode == SelectionMode.Single)
                {
                    return(Enumerable.Repeat(listBox.SelectedItem, 1));
                }
                else
                {
                    return(listBox.SelectedItems);
                }
            }
            //else if (itemsControl.GetType().IsAssignableFrom(typeof(TreeView)))
            else if (typeof(TreeView).IsAssignableFrom(itemsControl.GetType()))
            {
                return(Enumerable.Repeat(((TreeView)itemsControl).SelectedItem, 1));
            }
            //else if (itemsControl.GetType().IsAssignableFrom(typeof(Selector)))
            else if (typeof(Selector).IsAssignableFrom(itemsControl.GetType()))
            {
                return(Enumerable.Repeat(((Selector)itemsControl).SelectedItem, 1));
            }
            else
            {
                return(Enumerable.Empty <object>());
            }
        }
コード例 #4
0
        public static Type GetItemContainerType(this ItemsControl itemsControl, out bool isItemContainer)
        {
            ItemsControlInformation info = FindItemsControlInformation(itemsControl);

            // determines if the itemsControl is not a ListView, ListBox or TreeView
            isItemContainer = false;

            if (null != info)
            {
                return(info.ItemContainerType);
            }

            if (typeof(DataGrid).IsAssignableFrom(itemsControl.GetType()))
            {
                return(typeof(DataGridRow));
            }

            // There is no safe way to get the item container type for an ItemsControl.
            // First hard-code the types for the common ItemsControls.
            //if (itemsControl.GetType().IsAssignableFrom(typeof(ListView)))
            if (typeof(ListView).IsAssignableFrom(itemsControl.GetType()))
            {
                return(typeof(ListViewItem));
            }
            //if (itemsControl.GetType().IsAssignableFrom(typeof(ListBox)))
            else if (typeof(ListBox).IsAssignableFrom(itemsControl.GetType()))
            {
                return(typeof(ListBoxItem));
            }
            //else if (itemsControl.GetType().IsAssignableFrom(typeof(TreeView)))
            else if (typeof(TreeView).IsAssignableFrom(itemsControl.GetType()))
            {
                return(typeof(TreeViewItem));
            }

            // Otherwise look for the control's ItemsPresenter, get it's child panel and the first
            // child of that *should* be an item container.
            //
            // If the control currently has no items, we're out of luck.
            if (itemsControl.Items.Count > 0)
            {
                var itemsPresenters = itemsControl.GetVisualDescendents <ItemsPresenter>();

                foreach (var itemsPresenter in itemsPresenters)
                {
                    var panel         = VisualTreeHelper.GetChild(itemsPresenter, 0);
                    var itemContainer = VisualTreeHelper.GetChildrenCount(panel) > 0
                                ? VisualTreeHelper.GetChild(panel, 0)
                                : null;

                    // Ensure that this actually *is* an item container by checking it with
                    // ItemContainerGenerator.
                    if (itemContainer != null &&
                        itemsControl.ItemContainerGenerator.IndexFromContainer(itemContainer) != -1)
                    {
                        isItemContainer = true;
                        return(itemContainer.GetType());
                    }
                }
            }

            return(null);
        }