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

IndexFromGeneratorPosition() public method

public IndexFromGeneratorPosition ( GeneratorPosition position ) : int
position GeneratorPosition
return int
		public int IndexFromGeneratorPosition (GeneratorPosition position)
		{
			// We either have everything realised or nothing realised, so we can
			// simply just add Index and Offset together to get the right index (i think)
			if (position.Index == -1) {
				if (position.Offset < 0)
					return Owner.Items.Count + position.Offset;
				//else if (position.Offset == 0)
				//	return 0;
				else
					return position.Offset - 1;
			} else {
				if (position.Index > Owner.Items.Count)
					return -1;
				if (position.Index >= 0 && position.Index < RealizedElements.Count)
					return RealizedElements [position.Index] + position.Offset;
				return position.Index + position.Offset;
			}
		}

Usage Example

Example #1
0
        void AddItemsToPresenter(GeneratorPosition position, int count)
        {
            if (_presenter == null || _presenter._elementRoot == null || _presenter._elementRoot is VirtualizingPanel)
            {
                return;
            }

            Panel panel    = _presenter._elementRoot;
            int   newIndex = ItemContainerGenerator.IndexFromGeneratorPosition(position);

            using (var p = ItemContainerGenerator.StartAt(position, GeneratorDirection.Forward, true))
                for (int i = 0; i < count; i++)
                {
                    var item = Items [newIndex + i];
                    DependencyObject container = null;

                    bool fresh;
                    container = ItemContainerGenerator.GenerateNext(out fresh);
                    ContentControl c = container as ContentControl;
                    if (c != null)
                    {
                        c.ContentSetsParent = false;
                    }

                    FrameworkElement f = container as FrameworkElement;
                    if (f != null && !(item is FrameworkElement))
                    {
                        f.DataContext = item;
                    }

                    panel.Children.Insert(newIndex + i, (UIElement)container);
                    ItemContainerGenerator.PrepareItemContainer(container);
                }
        }