ContainerFromIndex() public method

public ContainerFromIndex ( int index ) : DependencyObject
index int
return DependencyObject
示例#1
0
 /// <summary>
 /// Returns a sequence of rating items.
 /// </summary>
 /// <returns>A sequence of rating items.</returns>
 internal IEnumerable <RatingItem> GetRatingItems()
 {
     return
         (Enumerable
          .Range(0, this.Items.Count)
          .Select(index => (RatingItem)ItemContainerGenerator.ContainerFromIndex(index))
          .Where(ratingItem => ratingItem != null));
 }
        /// <summary>
        /// Focus the first item in the TreeView.
        /// </summary>
        /// <returns>A value indicating whether the item was focused.</returns>
        private bool FocusFirstItem()
        {
            // Get the first item in the TreeView.
            TreeViewItem item = ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;

            return((item != null) ?
                   (item.IsEnabled && item.Focus()) || item.FocusDown() :
                   false);
        }
示例#3
0
        internal virtual void OnItemTemplateChanged(DataTemplate oldValue, DataTemplate newValue)
        {
            int count = Items.Count;

            for (int i = 0; i < count; i++)
            {
                UpdateContentTemplateOnContainer(ItemContainerGenerator.ContainerFromIndex(i), Items [i]);
            }
        }
示例#4
0
        public UIElement ContainerFromIndex(int itemIndex)
        {
            ItemContainerGenerator itemContainerGenerator = base.ItemContainerGenerator as ItemContainerGenerator;

            if (itemContainerGenerator != null)
            {
                return(itemContainerGenerator.ContainerFromIndex(itemIndex) as UIElement);
            }
            return(null);
        }
 /// <summary>
 /// Focus the last item in the TreeView.
 /// </summary>
 /// <returns>A value indicating whether the item was focused.</returns>
 private bool FocusLastItem()
 {
     for (int i = Items.Count - 1; i >= 0; i--)
     {
         TreeViewItem item = ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
         if (item != null && item.IsEnabled)
         {
             return(item.FocusInto());
         }
     }
     return(false);
 }
示例#6
0
文件: ListBox.cs 项目: ynkbt/moon
        internal override void OnItemContainerStyleChanged(Style oldStyle, Style newStyle)
        {
            int count = Items.Count;

            for (int i = 0; i < count; i++)
            {
                ListBoxItem item = (ListBoxItem)ItemContainerGenerator.ContainerFromIndex(i);
                if (item != null && item.Style == oldStyle)
                {
                    item.Style = newStyle;
                }
            }
        }
示例#7
0
        void OnDisplayMemberPathChanged(string oldPath, string newPath)
        {
            // refresh the display member template.
            displayMemberTemplate = null;
            var newTemplate = DisplayMemberTemplate;

            int count = Items.Count;

            for (int i = 0; i < count; i++)
            {
                UpdateContentTemplateOnContainer(ItemContainerGenerator.ContainerFromIndex(i), Items [i]);
            }
        }
示例#8
0
        /// <summary>
        ///     Returns a sequence of rating items.
        /// </summary>
        /// <returns>A sequence of rating items.</returns>
        internal IEnumerable <RatingItem> GetRatingItems()
        {
#if SILVERLIGHT
            return
                (Enumerable
                 .Range(0, this.Items.Count)
                 .Select(index => (RatingItem)ItemContainerGenerator.ContainerFromIndex(index))
                 .Where(ratingItem => ratingItem != null));
#else
            // The query above returns null in WPF
            // Either way, WPF will already contain the RatingItem objects in the Items collection.
            return(Items.Cast <RatingItem>());
#endif
        }
示例#9
0
        internal override void OnItemContainerStyleChanged(Style oldStyle, Style newStyle)
        {
            int count = Items.Count;

            for (int i = 0; i < count; i++)
            {
                var item      = Items [i];
                var container = (ListBoxItem)ItemContainerGenerator.ContainerFromIndex(i);
                if (container != null && item != container)
                {
                    container.Style = newStyle;
                }
            }
        }
示例#10
0
文件: TreeView.cs 项目: ash2005/z
 private bool GetFirstItem(out object item, out TreeViewItem container)
 {
     if (HasItems)
     {
         item      = Items[0];
         container = ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
         return((item != null) && (container != null));
     }
     else
     {
         item      = null;
         container = null;
         return(false);
     }
 }
示例#11
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);
        }
示例#12
0
        /// <summary>
        ///     Method which generates the focus trail cell
        ///     if it is not already generated and adds it to 
        ///     the block lists appropriately.
        /// </summary>
        private bool GenerateChildForFocusTrail(
            ItemContainerGenerator generator,
            List<int> realizedColumnIndices,
            List<int> realizedColumnDisplayIndices,
            Size constraint,
            int displayIndex,
            ref int displayIndexListIterator)
        {
            DataGrid dataGrid = ParentDataGrid;
            DataGridColumn column = dataGrid.ColumnFromDisplayIndex(displayIndex);
            if (column.IsVisible)
            {
                int columnIndex = dataGrid.ColumnIndexFromDisplayIndex(displayIndex);

                UIElement child = generator.ContainerFromIndex(columnIndex) as UIElement;
                if (child == null)
                {
                    int childIndex = columnIndex;
                    Size childSize;
                    using (((IItemContainerGenerator)generator).StartAt(IndexToGeneratorPositionForStart(generator, childIndex, out childIndex), GeneratorDirection.Forward, true))
                    {
                        child = GenerateChild(generator, constraint, column, ref childIndex, out childSize);
                    }
                }

                if (child != null && DataGridHelper.TreeHasFocusAndTabStop(child))
                {
                    AddToIndicesListIfNeeded(
                        realizedColumnIndices,
                        realizedColumnDisplayIndices,
                        columnIndex,
                        displayIndex,
                        ref displayIndexListIterator);
                    return true;
                }
            }

            return false;
        }
示例#13
0
            // update container and index with current values
            internal ItemInfo Refresh(ItemContainerGenerator generator)
            {
                if (Container == null && Index < 0)
                {
                    Container = generator.ContainerFromItem(Item);
                }

                if (Index < 0 && Container != null)
                {
                    Index = generator.IndexFromContainer(Container);
                }

                if (Container == null && Index >= 0)
                {
                    Container = generator.ContainerFromIndex(Index);
                }

                if (Container == SentinelContainer && Index >= 0)
                {
                    Container = null;   // caller explicitly wants null container
                }

                return this;
            }
示例#14
0
文件: ListBox.cs 项目: ynkbt/moon
        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;
                }
            }
        }
示例#15
0
 private ListBoxItem ElementAt(int index)
 {
     return(ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem);
 }
示例#16
0
 /// <summary>
 /// Treeview doesn't have a simple clear selection
 /// </summary>
 /// <remarks>
 /// Got this here:
 /// http://stackoverflow.com/questions/676819/wpf-treeview-clear-selection
 /// </remarks>
 private static void ClearTreeViewSelection(ItemCollection items, ItemContainerGenerator containerGenerator)
 {
     if (items != null && containerGenerator != null)
     {
         for (int cntr = 0; cntr < items.Count; cntr++)
         {
             // Can't use item directly, because it could be any UIElement
             TreeViewItem itemContainer = containerGenerator.ContainerFromIndex(cntr) as TreeViewItem;
             if (itemContainer != null)
             {
                 // Recurse
                 ClearTreeViewSelection(itemContainer.Items, itemContainer.ItemContainerGenerator);
                 itemContainer.IsSelected = false;
             }
         }
     }
 }
示例#17
0
        void UpdateDisplayedItem(object selectedItem)
        {
            object content;

            // Can't do anything with no content presenter
            if (_contentPresenter == null)
            {
                return;
            }

            // Return the currently displayed object (which is a UIElement)
            // to its original container.
            if (DisplayedItem != null)
            {
                content = _contentPresenter.Content;
                DisplayedItem.Content = content;
                DisplayedItem         = null;
            }
            _contentPresenter.Content = null;

            if (selectedItem == null)
            {
                _contentPresenter.Content         = NothingSelectedFallback;
                _contentPresenter.ContentTemplate = null;
                SelectionBoxItem         = null;
                SelectionBoxItemTemplate = null;
                return;
            }

            // If the currently selected item is a ComboBoxItem (not ListBoxItem!), we
            // display its Content instead of the CBI itself.
            content = selectedItem;
            if (content is ComboBoxItem)
            {
                content = ((ComboBoxItem)content).Content;
            }

            // Only allow DisplayedItem to be non-null if we physically move
            // its content. This will only happen if DisplayedItem == SelectedItem
            DisplayedItem = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as ComboBoxItem;

            SelectionBoxItem         = content;
            SelectionBoxItemTemplate = ItemTemplate;

            // If displayed item is avaiable, we can get the right template from there. Otherwise
            // we need to create a container, read the template and destroy it.
            if (DisplayedItem != null)
            {
                SelectionBoxItemTemplate = DisplayedItem.ContentTemplate;
                if (content is UIElement)
                {
                    DisplayedItem.Content = null;
                }
                else
                {
                    DisplayedItem = null;
                }
            }
            else
            {
                bool         fresh;
                ComboBoxItem container = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as ComboBoxItem;
                if (container == null)
                {
                    var index = ItemContainerGenerator.GeneratorPositionFromIndex(SelectedIndex);
                    using (ItemContainerGenerator.StartAt(index, GeneratorDirection.Forward, true))
                        container = ItemContainerGenerator.GenerateNext(out fresh) as ComboBoxItem;
                    ItemContainerGenerator.PrepareItemContainer(container);
                }
                SelectionBoxItemTemplate = container.ContentTemplate;
            }

            _contentPresenter.Content         = SelectionBoxItem;
            _contentPresenter.ContentTemplate = SelectionBoxItemTemplate;
        }
示例#18
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (!e.Handled)
            {
                var key = e.Key;
                if (FlowDirection == FlowDirection.RightToLeft)
                {
                    if (key == Key.Left)
                    {
                        key = Key.Right;
                    }
                    else if (key == Key.Right)
                    {
                        key = Key.Left;
                    }
                }

                e.Handled = true;
                switch (key)
                {
                case Key.Escape:
                    IsDropDownOpen = false;
                    break;

                case Key.Enter:
                case Key.Space:
                    if (IsDropDownOpen && FocusedIndex != SelectedIndex)
                    {
                        SelectedIndex  = FocusedIndex;
                        IsDropDownOpen = false;
                    }
                    else
                    {
                        IsDropDownOpen = true;
                    }
                    break;

                case Key.Right:
                case Key.Down:
                    if (IsDropDownOpen)
                    {
                        if (FocusedIndex < Items.Count - 1)
                        {
                            FocusedIndex++;
                            ((Control)ItemContainerGenerator.ContainerFromIndex(FocusedIndex)).Focus();
                        }
                    }
                    else
                    {
                        SelectedIndex = Math.Min(SelectedIndex + 1, Items.Count - 1);
                    }
                    break;

                case Key.Left:
                case Key.Up:
                    if (IsDropDownOpen)
                    {
                        if (FocusedIndex > 0)
                        {
                            FocusedIndex--;
                            ((Control)ItemContainerGenerator.ContainerFromIndex(FocusedIndex)).Focus();
                        }
                    }
                    else if (SelectedIndex != -1)
                    {
                        SelectedIndex = Math.Max(SelectedIndex - 1, 0);
                    }
                    break;

                default:
                    e.Handled = false;
                    break;
                }
            }
            else
            {
                Console.WriteLine("Already handled");
            }
        }
示例#19
0
        void IsDropDownOpenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            bool open = (bool)e.NewValue;

            if (_popup != null)
            {
                _popup.IsOpen = open;
            }
            if (_dropDownToggle != null)
            {
                _dropDownToggle.IsChecked = open;
            }

            if (open)
            {
                ComboBoxItem t = null;
                FocusedIndex = Items.Count > 0 ? Math.Max(SelectedIndex, 0) : -1;
                if (FocusedIndex > -1)
                {
                    t = ItemContainerGenerator.ContainerFromIndex(FocusedIndex) as ComboBoxItem;
                }

                // If the ItemsPresenter hasn't attached yet 't' will be null.
                // When the itemsPresenter attaches, focus will be set when the
                // item is loaded
                if (t != null)
                {
                    t.Focus();
                }

                LayoutUpdated += UpdatePopupSizeAndPosition;

                OnDropDownOpened(EventArgs.Empty);

                // Raises UIA Event
                AutomationPeer peer = ((ComboBox)sender).AutomationPeer;
                if (peer != null)
                {
                    peer.RaisePropertyChangedEvent(ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty,
                                                   ExpandCollapseState.Collapsed,
                                                   ExpandCollapseState.Expanded);
                }
            }
            else
            {
                Focus();

                LayoutUpdated -= UpdatePopupSizeAndPosition;

                OnDropDownClosed(EventArgs.Empty);

                // Raises UIA Event
                AutomationPeer peer = ((ComboBox)sender).AutomationPeer;
                if (peer != null)
                {
                    peer.RaisePropertyChangedEvent(ExpandCollapsePatternIdentifiers.ExpandCollapseStateProperty,
                                                   ExpandCollapseState.Expanded,
                                                   ExpandCollapseState.Collapsed);
                }
            }

            UpdateDisplayedItem(open && SelectedItem is UIElement ? null : SelectedItem);
            UpdateVisualState(true);
        }