ItemFromContainer() public method

public ItemFromContainer ( DependencyObject container ) : object
container DependencyObject
return object
Ejemplo n.º 1
0
        private TabItem GetSelectedTabItem()
        {
            object selectedItem = SelectedItem;

            if (selectedItem != null)
            {
                // Check if the selected item is a TabItem
                TabItem tabItem = selectedItem as TabItem;
                if (tabItem == null)
                {
                    // It is a data item, get its TabItem container
                    tabItem = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as TabItem;

                    // Due to event leapfrogging, we may have the wrong container.
                    // If so, re-fetch the right container using a more expensive method.
                    // (BTW, the previous line will cause a debug assert in this case)  [Dev10 452711]
                    if (tabItem == null ||
                        !ItemsControl.EqualsEx(selectedItem, ItemContainerGenerator.ItemFromContainer(tabItem)))
                    {
                        tabItem = ItemContainerGenerator.ContainerFromItem(selectedItem) as TabItem;
                    }
                }

                return(tabItem);
            }

            return(null);
        }
Ejemplo n.º 2
0
        private void SetSingleSelection(ListBoxItem item)
        {
            SelectedItem = ItemContainerGenerator.ItemFromContainer(item);

            for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++)
            {
                DependencyObject itemContainer = ItemContainerGenerator.Generate(i);
                itemContainer.SetCurrentValue(Selector.IsSelectedProperty, itemContainer == item);
            }
        }
Ejemplo n.º 3
0
        private void ToggleSingleSelection(ListBoxItem item)
        {
            bool isSelected = !(bool)item.GetValue(Selector.IsSelectedProperty);

            SelectedItem = isSelected ? ItemContainerGenerator.ItemFromContainer(item) : null;

            for (int i = 0; i < ItemContainerGenerator.ItemsCount; i++)
            {
                DependencyObject itemContainer = ItemContainerGenerator.Generate(i);
                itemContainer.SetCurrentValue(Selector.IsSelectedProperty, itemContainer == item && isSelected);
            }
        }
Ejemplo n.º 4
0
        public int IndexFromContainer(UIElement container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            ItemContainerGenerator itemContainerGenerator = base.ItemContainerGenerator as ItemContainerGenerator;

            if (itemContainerGenerator != null && itemContainerGenerator.ItemFromContainer(container) != DependencyProperty.UnsetValue)
            {
                object obj = container.ReadLocalValue(IndexForItemContainerProperty);
                return((obj as int?) ?? (-1));
            }
            return(-1);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Select the first item of the TreeView.
        /// </summary>
        private void SelectFirstItem()
        {
            TreeViewItem container = ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
            bool         found     = container != null;

            if (!found)
            {
                container = SelectedContainer;
            }

            object item = found ?
                          ItemContainerGenerator.ItemFromContainer(container) :
                          SelectedItem;

            ChangeSelection(item, container, found);
        }
Ejemplo n.º 6
0
        public object ItemFromContainer(UIElement container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            ItemContainerGenerator itemContainerGenerator = base.ItemContainerGenerator as ItemContainerGenerator;

            if (itemContainerGenerator != null)
            {
                object obj = itemContainerGenerator.ItemFromContainer(container);
                if (obj == DependencyProperty.UnsetValue)
                {
                    return(null);
                }
                return(obj);
            }
            return(null);
        }
Ejemplo n.º 7
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (!e.Handled)
            {
                bool handled         = false;
                int  newFocusedIndex = -1;
                switch (e.Key)
                {
                case Key.Space:
                case Key.Enter:
                    if ((Key.Enter != e.Key) || KeyboardNavigation.GetAcceptsReturn(this))
                    {
                        if (ModifierKeys.Alt != (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Alt)))
                        {
                            // KeyEventArgs.OriginalSource (used by WPF) isn't available in Silverlight; use FocusManager.GetFocusedElement instead
                            ListBoxItem listBoxItem = FocusManager.GetFocusedElement() as ListBoxItem;
                            if (null != listBoxItem)
                            {
                                if ((ModifierKeys.Control == (Keyboard.Modifiers & ModifierKeys.Control)) && listBoxItem.IsSelected)
                                {
                                    SelectedItem = null;
                                }
                                else
                                {
                                    SelectedItem = ItemContainerGenerator.ItemFromContainer(listBoxItem);
                                }
                                handled = true;
                            }
                        }
                    }
                    break;

                case Key.Home:
                    newFocusedIndex = 0;
                    break;

                case Key.End:
                    newFocusedIndex = Items.Count - 1;
                    break;

                case Key.PageUp:
                    newFocusedIndex = NavigateByPage(false);
                    break;

                case Key.PageDown:
                    newFocusedIndex = NavigateByPage(true);
                    break;

                case Key.Left:
                    if (IsVerticalOrientation())
                    {
                        ElementScrollViewerScrollInDirection(Key.Left);
                    }
                    else
                    {
                        newFocusedIndex = _focusedIndex - 1;
                    }
                    break;

                case Key.Up:
                    if (IsVerticalOrientation())
                    {
                        newFocusedIndex = _focusedIndex - 1;
                    }
                    else
                    {
                        ElementScrollViewerScrollInDirection(Key.Up);
                    }
                    break;

                case Key.Right:
                    if (IsVerticalOrientation())
                    {
                        ElementScrollViewerScrollInDirection(Key.Right);
                    }
                    else
                    {
                        newFocusedIndex = _focusedIndex + 1;
                    }
                    break;

                case Key.Down:
                    if (IsVerticalOrientation())
                    {
                        newFocusedIndex = _focusedIndex + 1;
                    }
                    else
                    {
                        ElementScrollViewerScrollInDirection(Key.Down);
                    }
                    break;

                // case Key.Divide:  // Feature only used by SelectionMode.Extended
                // case Key.Oem2:  // Key not supported by Silverlight
                //    break;
                // case Key.Oem5:  // Key not supported by Silverlight
                //     break;
                default:
                    Debug.Assert(!handled);
                    break;
                }
                if ((-1 != newFocusedIndex) &&
                    (-1 != _focusedIndex) &&
                    (newFocusedIndex != _focusedIndex) &&
                    (0 <= newFocusedIndex) &&
                    (newFocusedIndex < Items.Count))
                {
                    // A key press will change the focused ListBoxItem
                    ListBoxItem listBoxItem = (ListBoxItem)ItemContainerGenerator.ContainerFromIndex(newFocusedIndex);
                    Debug.Assert(null != listBoxItem);
                    ScrollIntoView(ItemContainerGenerator.ItemFromContainer(listBoxItem));
                    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    {
                        listBoxItem.Focus();
                    }
                    else
                    {
                        SelectedItem = ItemContainerGenerator.ItemFromContainer(listBoxItem);
                    }
                    handled = true;
                }
                if (handled)
                {
                    e.Handled = true;
                }
            }
        }