public void GetItemAt() { object[] list = { 1, 2, 3, 4, 5, 6 }; var cv = new CollectionView(list); Assert.AreEqual(4, cv.GetItemAt(3), "A1"); Assert.IsNull(cv.GetItemAt(99), "A2"); }
private bool GetNativeItemAt(int index, out object value) { bool isNativeValue = false; value = null; if (List != null) { value = List[index]; isNativeValue = true; } else if (CollectionView != null) { value = CollectionView.GetItemAt(index); isNativeValue = true; } else if (_reflectedItemAt != null) { try { value = _reflectedItemAt.GetValue(Enumerable, new object[] { index }); isNativeValue = true; } catch (MethodAccessException) { // revert to walking the IEnumerable // under partial trust, some properties are not accessible even though they are public // see bug 1415832 _reflectedItemAt = null; isNativeValue = false; } } return(isNativeValue); }
/// <summary> /// Returns the item at the specified zero-based index in this view. /// </summary> /// <param name="index">The zero-based index at which the item is located.</param> /// <returns>The item at the specified zero-based index in this view.</returns> public override Object GetItemAt(Int32 index) { // Make sure the index is within range. For performance reasons there is no check for the upper bounds of an IEnumerable collection. if (index < 0) { throw new ArgumentOutOfRangeException("index"); } // See if we can extract an item from a collection view. CollectionView collectionView = this.currentView as CollectionView; if (collectionView != null) { return(collectionView.GetItemAt(index)); } // See if there is a ViewableCollection class that can be used to return the number of items in the collection. ViewableCollection viewableCollection = this.currentView as ViewableCollection; if (viewableCollection != null) { return(viewableCollection.GetItemAt(index)); } // At this point, we can't extract an item from the current collection view. return(null); }
private void SearchElement_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (e.Key == Key.Down || e.Key == Key.Up) { ((ComboBoxItem)this.ItemContainerGenerator.ContainerFromIndex(0))?.Focus(); } else if (e.Key == Key.Enter && itemsViewOriginal != null && itemsViewOriginal.Count > 0) { SelectedItem = itemsViewOriginal.GetItemAt(0); } }
public void GetItemAtTest() { // store off the first item in the list for comparing later in the test TestClass firstItem = CollectionView[0] as TestClass; // check that we get the correct item for each index in the source collection int index = 0; foreach (TestClass item in SourceCollection) { Assert.AreEqual(item, CollectionView.GetItemAt(index)); index++; } // check that for an index not in the collection, we throw an argument // out of range exception AssertExpectedException( new ArgumentOutOfRangeException("index"), delegate { CollectionView.GetItemAt(-1); }); AssertExpectedException( new ArgumentOutOfRangeException("index"), delegate { CollectionView.GetItemAt(25); }); // now check that with paging, we return the item relative // to the index of the page its on. CollectionView.PageSize = 5; index = 0; foreach (TestClass item in SourceCollection) { Assert.AreEqual(item, CollectionView.GetItemAt(index % 5)); index++; // move to the next page after we go through all the items // on the current page if (index % 5 == 0) { CollectionView.MoveToNextPage(); } } }
/// <summary> /// Handles the event which occurs just before a pie piece tooltip opens /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void PiePieceToolTipOpening(object sender, ToolTipEventArgs e) { PiePiece piece = (PiePiece)sender; CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (collectionView == null) { return; } // select the item which this pie piece represents int index = (int)piece.Tag; if (piece.ToolTip != null) { ToolTip tip = (ToolTip)piece.ToolTip; tip.DataContext = collectionView.GetItemAt(index); } }
private bool AllMessagesMatch(string[] messages) { CollectionView items = (CollectionView)ErrorsText.Items; if (items.Count != messages.Length) { return(false); } for (int i = 0; i < messages.Length; i++) { NotificationHeaderControl item = (NotificationHeaderControl)items.GetItemAt(i); string path = BindingOperations.GetBinding(item, HeaderedContentControl.ContentProperty).Path.Path; if (!path.Equals(messages[i])) { return(false); } } return(true); }
private bool GetNativeItemAt(int index, out object value) { var isNativeValue = false; value = null; if (List != null) { value = List[index]; isNativeValue = true; } else if (ReadOnlyList != null) { value = ReadOnlyList[index]; isNativeValue = true; } else if (CollectionView != null) { value = CollectionView.GetItemAt(index); isNativeValue = true; } else if (_reflectedItemAt != null) { try { value = _reflectedItemAt.GetValue(Enumerable, new object[] { index }); isNativeValue = true; } catch (MethodAccessException) { _reflectedItemAt = null; isNativeValue = false; } } return(isNativeValue); }
public void OffsetIndexBasedOnSourceNewIndex(int maxOffset) { if (m_dataGridContext == null) { throw new InvalidOperationException("We must have a DataGridContext to find the new index."); } CollectionView sourceItems = m_dataGridContext.Items; for (int i = this.Count - 1; i >= 0; i--) { SelectionCellRangeWithItems cellRangeWithItems = m_list[i]; SelectionRangeWithItems itemRangeWithItems = cellRangeWithItems.ItemRangeWithItems; object[] items = itemRangeWithItems.Items; if (items == null) { throw new InvalidOperationException("We should have items to find the new index."); } object item = items[0]; SelectionRange itemRange = itemRangeWithItems.Range; int startIndex = Math.Min(itemRange.StartIndex, itemRange.EndIndex); if (maxOffset < 0) { for (int j = 0; j >= maxOffset; j--) { if (object.Equals(sourceItems.GetItemAt(startIndex), item)) { if (j != 0) { SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + j, itemRange.EndIndex + j); m_list[i] = new SelectionCellRangeWithItems(newItemRange, items, cellRangeWithItems.ColumnRange); } break; } startIndex--; if (startIndex < 0) { break; } } } else { int sourceItemCount = sourceItems.Count; for (int j = 0; j <= maxOffset; j++) { if (object.Equals(sourceItems.GetItemAt(startIndex), item)) { if (j != 0) { SelectionRange newItemRange = new SelectionRange(itemRange.StartIndex + j, itemRange.EndIndex + j); m_list[i] = new SelectionCellRangeWithItems(newItemRange, items, cellRangeWithItems.ColumnRange); } break; } startIndex++; if (startIndex >= sourceItemCount) { break; } } } } }
public void Visit(DataGridContext sourceContext, ref bool stopVisit) { object[] items; CollectionView itemsCollection = sourceContext.Items; int count = itemsCollection.Count; if (count == 0) { return; } if (sourceContext.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase) { items = null; } else { items = new object[count]; for (int i = 0; i < count; i++) { items[i] = itemsCollection.GetItemAt(i); } } SelectionRange itemRange = new SelectionRange(0, count - 1); if (sourceContext.DataGridControl.SelectionUnit == SelectionUnit.Row) { sourceContext.DataGridControl.SelectionChangerManager.SelectItems( sourceContext, new SelectionRangeWithItems(itemRange, items)); } else { HashedLinkedList <ColumnBase> columnsByVisiblePosition = sourceContext.ColumnsByVisiblePosition; SelectedItemsStorage selectedColumnStore = new SelectedItemsStorage(null); SelectionRange fullColumnRange = new SelectionRange(0, columnsByVisiblePosition.Count - 1); selectedColumnStore.Add(new SelectionRangeWithItems(fullColumnRange, null)); int index = 0; foreach (ColumnBase column in columnsByVisiblePosition) { if (!column.Visible) { selectedColumnStore.Remove(new SelectionRangeWithItems(new SelectionRange(index), null)); } index++; } int columnRangeCount = selectedColumnStore.Count; for (int i = 0; i < columnRangeCount; i++) { sourceContext.DataGridControl.SelectionChangerManager.SelectCells( sourceContext, new SelectionCellRangeWithItems(itemRange, items, selectedColumnStore[i].Range)); } } }
private AutomationPeer GetPeerForChildrenIndex(int index) { int fixedHeadersCount = 0; Panel fixedHeadersPanel = null; int fixedFootersCount = 0; Panel fixedFootersPanel = null; if (m_parentDataGridContext == null) { fixedHeadersPanel = m_dataGridControl.FixedHeadersHostPanel; fixedHeadersCount = (fixedHeadersPanel != null) ? fixedHeadersPanel.Children.Count : 0; fixedFootersPanel = m_dataGridControl.FixedFootersHostPanel; fixedFootersCount = (fixedFootersPanel != null) ? fixedFootersPanel.Children.Count : 0; } if (index < fixedHeadersCount) { UIElement element = fixedHeadersPanel.Children[index]; AutomationPeer automationPeer = m_headerFooterChildren[element] as AutomationPeer; Debug.Assert(automationPeer != null); if (automationPeer == null) { return(null); } return(automationPeer); } else { index -= fixedHeadersCount; HeadersFootersGeneratorNode headerNode = m_dataGridContext.CustomItemContainerGenerator.Header; int headersCount = (headerNode == null) ? 0 : headerNode.ItemCount; if (index < headersCount) { object item = headerNode.GetAt(index); AutomationPeer automationPeer = m_headerFooterChildren[item] as AutomationPeer; Debug.Assert(automationPeer != null); return(automationPeer); } else { index -= headersCount; CollectionView items = m_dataGridContext.Items; int itemsCount = (items == null) ? 0 : items.Count; if (index < itemsCount) { return(this.FindOrCreateItemAutomationPeer(items.GetItemAt(index), index)); } else { index -= itemsCount; HeadersFootersGeneratorNode footerNode = m_dataGridContext.CustomItemContainerGenerator.Footer; int footersCount = (footerNode == null) ? 0 : footerNode.ItemCount; if (index < footersCount) { object item = footerNode.GetAt(index); AutomationPeer automationPeer = m_headerFooterChildren[item] as AutomationPeer; Debug.Assert(automationPeer != null); return(automationPeer); } else { index -= footersCount; if (index < fixedFootersCount) { UIElement element = fixedFootersPanel.Children[index]; AutomationPeer automationPeer = m_headerFooterChildren[element] as AutomationPeer; Debug.Assert(automationPeer != null); return(automationPeer); } } } } } return(null); }
public void GetItemAtLessThan0() { var cv = new CollectionView(new object[] { }); cv.GetItemAt(-1); }
public static void UpdateSelectionRangeWithItemsFromAdd( DataGridContext dataGridContext, SelectionRangeWithItems newRangeWithItems, ref SelectionRangeWithItems lastRangeWithItems) { // Only work for adding at the end of an existing range. object[] newRangeItems = newRangeWithItems.Items; if (((dataGridContext == null) || ((dataGridContext.ItemsSourceCollection == null) && (newRangeItems == null)) || (dataGridContext.ItemsSourceCollection is DataGridVirtualizingCollectionViewBase))) { lastRangeWithItems = new SelectionRangeWithItems( new SelectionRange(lastRangeWithItems.Range.StartIndex, newRangeWithItems.Range.EndIndex), null); return; } object[] rangeToUpdateItems = lastRangeWithItems.Items; object[] newItems; int newItemsStartPosition; if (newRangeWithItems == lastRangeWithItems) { if (newRangeItems != null) { return; } newItemsStartPosition = 0; newItems = new object[lastRangeWithItems.Length]; } else { newItemsStartPosition = lastRangeWithItems.Length; newItems = new object[lastRangeWithItems.Length + newRangeWithItems.Length]; Array.Copy(rangeToUpdateItems, newItems, newItemsStartPosition); } CollectionView items = (dataGridContext == null) ? null : dataGridContext.Items; int rangeToUpdateStartIndex = lastRangeWithItems.Range.StartIndex; int rangeToUpdateEndIndex = lastRangeWithItems.Range.EndIndex; int newRangeStartIndex = newRangeWithItems.Range.StartIndex; int newRangeEndIndex = newRangeWithItems.Range.EndIndex; // If new range have no items set, found the items and set it. if (newRangeItems == null) { if (newRangeEndIndex > newRangeStartIndex) { for (int i = newRangeStartIndex; i <= newRangeEndIndex; i++) { newItems[newItemsStartPosition] = items.GetItemAt(i); newItemsStartPosition++; } } else { for (int i = newRangeStartIndex; i >= newRangeEndIndex; i--) { newItems[newItemsStartPosition] = items.GetItemAt(i); newItemsStartPosition++; } } } else { for (int i = 0; i < newRangeItems.Length; i++) { newItems[newItemsStartPosition] = newRangeItems[i]; newItemsStartPosition++; } } lastRangeWithItems = new SelectionRangeWithItems( new SelectionRange(rangeToUpdateStartIndex, newRangeEndIndex), newItems); }
public void GetItemAtFilter () { CollectionView c = new CollectionView (new object [] { -1, 1 }); c.Filter = GetItemAtFilterFilter; Assert.AreEqual (c.GetItemAt (0), -1, "1"); Assert.AreEqual (c.GetItemAt (1), 1, "2"); try { c.GetItemAt (-1); Assert.Fail ("3"); } catch (ArgumentOutOfRangeException) { } try { c.GetItemAt (2); Assert.Fail ("4"); } catch (IndexOutOfRangeException) { } }