System.Windows.Controls.ItemContainerGenerator.GeneratorPositionFromIndex C# (CSharp) Method

GeneratorPositionFromIndex() public method

public GeneratorPositionFromIndex ( int itemIndex ) : GeneratorPosition
itemIndex int
return GeneratorPosition
		public GeneratorPosition GeneratorPositionFromIndex (int itemIndex)
		{
			if (itemIndex < 0) {
				return new GeneratorPosition (-1, 0);
			}
			else if (RealizedElements.Contains (itemIndex)) {
				return new GeneratorPosition (RealizedElements.IndexOf (itemIndex), 0);
			}
			else if (itemIndex > Owner.Items.Count) {
				return new GeneratorPosition (-1, 0);
			}

			if (RealizedElements.Count == 0)
				return new GeneratorPosition (-1, itemIndex + 1);

			int index = -1;
			for (int i = 0; i < RealizedElements.Count; i ++) {
				if (RealizedElements [i] > itemIndex)
					break;
				index = i;
			}
			if (index == -1) {
				return new GeneratorPosition (index, itemIndex + 1);
			} else {
				return new GeneratorPosition (index, itemIndex - RealizedElements [index]);
			}
		}

Usage Example

Esempio n. 1
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;
        }