I​Collection​View I​Collection​View I​Collection​View Interface

Definition

Enables collections to support current record management, grouping, and incremental loading (data virtualization).

public interface ICollectionViewpublic interface ICollectionViewPublic Interface ICollectionView
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

ICollectionView is an interface used to represent a collection. It serves to maintain currency for properties like item selection.

To implement custom behavior for selection currency in your data source, your data source should implement ICollectionViewFactory instead of implementing ICollectionView directly. You can set the CollectionViewSource.Source property to an ICollectionViewFactory, but if you set it to an ICollectionView, it will throw an exception.

The CollectionGroups property is of type IObservableVector;, but these objects should implement ICollectionViewGroup.

Interface inheritance

ICollectionView inherits IObservableVector, IVector and IIterable . Types that implement ICollectionView also implement the interface members of IObservableVector, and either IVector and IIterable for C++ usage or IList and IEnumerable for Microsoft .NET usage.

Properties

CollectionGroups CollectionGroups CollectionGroups

Returns any collection groups that are associated with the view.

public IObservableVector<object> CollectionGroups { get; }public IObservableVector<object> CollectionGroups { get; }Public ReadOnly Property CollectionGroups As IObservableVector<object>
Value

A vector collection of possible views.

Attributes

Remarks

The CollectionGroups property is of type IObservableVector;, but these objects should implement ICollectionViewGroup.

CurrentItem CurrentItem CurrentItem

Gets the current item in the view.

public PlatForm::Object CurrentItem { get; }public object CurrentItem { get; }Public ReadOnly Property CurrentItem As object
Value
object object object

The current item in the view or null if there is no current item.

Attributes

CurrentPosition CurrentPosition CurrentPosition

Gets the ordinal position of the CurrentItem within the view.

public int CurrentPosition { get; }public int CurrentPosition { get; }Public ReadOnly Property CurrentPosition As int
Value
int int int

The ordinal position of the CurrentItem within the view.

Attributes

HasMoreItems HasMoreItems HasMoreItems

Gets a sentinel value that supports incremental loading implementations. See also LoadMoreItemsAsync.

public PlatForm::Boolean HasMoreItems { get; }public bool HasMoreItems { get; }Public ReadOnly Property HasMoreItems As bool
Value
bool bool bool

true if additional unloaded items remain in the view; otherwise, false.

Attributes

IsCurrentAfterLast IsCurrentAfterLast IsCurrentAfterLast

Gets a value that indicates whether the CurrentItem of the view is beyond the end of the collection.

public PlatForm::Boolean IsCurrentAfterLast { get; }public bool IsCurrentAfterLast { get; }Public ReadOnly Property IsCurrentAfterLast As bool
Value
bool bool bool

true if the CurrentItem of the view is beyond the end of the collection; otherwise, false.

Attributes

IsCurrentBeforeFirst IsCurrentBeforeFirst IsCurrentBeforeFirst

Gets a value that indicates whether the CurrentItem of the view is beyond the beginning of the collection.

public PlatForm::Boolean IsCurrentBeforeFirst { get; }public bool IsCurrentBeforeFirst { get; }Public ReadOnly Property IsCurrentBeforeFirst As bool
Value
bool bool bool

true if the CurrentItem of the view is beyond the beginning of the collection; otherwise, false.

Attributes

Methods

LoadMoreItemsAsync(UInt32) LoadMoreItemsAsync(UInt32) LoadMoreItemsAsync(UInt32)

Initializes incremental loading from the view.

public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(UInt32 count)public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(UInt32 count)Public Function LoadMoreItemsAsync(count As UInt32) As IAsyncOperation( Of LoadMoreItemsResult )
Parameters
count
System.UInt32 System.UInt32 System.UInt32

The number of items to load.

Returns

The wrapped results of the load operation.

Attributes

MoveCurrentTo(Object) MoveCurrentTo(Object) MoveCurrentTo(Object)

Sets the specified item to be the CurrentItem in the view.

public PlatForm::Boolean MoveCurrentTo(Object item)public bool MoveCurrentTo(Object item)Public Function MoveCurrentTo(item As Object) As bool
Parameters
item
System.Object System.Object System.Object

The item to set as the CurrentItem.

Returns
bool bool bool

true if the resulting CurrentItem is within the view; otherwise, false.

Attributes

MoveCurrentToFirst() MoveCurrentToFirst() MoveCurrentToFirst()

Sets the first item in the view as the CurrentItem.

public PlatForm::Boolean MoveCurrentToFirst()public bool MoveCurrentToFirst()Public Function MoveCurrentToFirst() As bool
Returns
bool bool bool

true if the resulting CurrentItem is an item within the view; otherwise, false.

Attributes

MoveCurrentToLast() MoveCurrentToLast() MoveCurrentToLast()

Sets the last item in the view as the CurrentItem.

public PlatForm::Boolean MoveCurrentToLast()public bool MoveCurrentToLast()Public Function MoveCurrentToLast() As bool
Returns
bool bool bool

true if the resulting CurrentItem is an item within the view; otherwise, false.

Attributes

MoveCurrentToNext() MoveCurrentToNext() MoveCurrentToNext()

Sets the item after the CurrentItem in the view as the CurrentItem.

public PlatForm::Boolean MoveCurrentToNext()public bool MoveCurrentToNext()Public Function MoveCurrentToNext() As bool
Returns
bool bool bool

true if the resulting CurrentItem is an item within the view; otherwise, false.

Attributes

MoveCurrentToPosition(Int32) MoveCurrentToPosition(Int32) MoveCurrentToPosition(Int32)

Sets the item at the specified index to be the CurrentItem in the view.

public PlatForm::Boolean MoveCurrentToPosition(Int32 index)public bool MoveCurrentToPosition(Int32 index)Public Function MoveCurrentToPosition(index As Int32) As bool
Parameters
index
System.Int32 System.Int32 System.Int32

The index of the item to move to.

Returns
bool bool bool

true if the resulting CurrentItem is an item within the view; otherwise, false.

Attributes

MoveCurrentToPrevious() MoveCurrentToPrevious() MoveCurrentToPrevious()

Sets the item before the CurrentItem in the view as the CurrentItem.

public PlatForm::Boolean MoveCurrentToPrevious()public bool MoveCurrentToPrevious()Public Function MoveCurrentToPrevious() As bool
Returns
bool bool bool

true if the resulting CurrentItem is an item within the view; otherwise, false.

Attributes

Events

CurrentChanged CurrentChanged CurrentChanged

When implementing this interface, fire this event after the current item has been changed.

public abstract event EventHandler CurrentChangedpublic abstract event EventHandler CurrentChangedPublic MustInherit Event CurrentChanged
Attributes

Remarks

Don't implement this interface for Microsoft .NET. See Remarks in ICollectionView.

CurrentChanging CurrentChanging CurrentChanging

When implementing this interface, fire this event before changing the current item. The event handler can cancel this event.

public abstract event CurrentChangingEventHandler CurrentChangingpublic abstract event CurrentChangingEventHandler CurrentChangingPublic MustInherit Event CurrentChanging
Attributes

Remarks

The CurrentChanging event occurs when the CurrentItem property value is changing. The CurrentChangingEventArgs parameter passed to the event handler specifies information about the change. If IsCancelable is true, the event handler can cancel the change by setting Cancel to true. If the change is canceled, CurrentItem is not changed. Setting Cancel to true when IsCancelable is false throws an exception.

Classes that implement this interface should fire the CurrentChanging event, set IsCancelable as appropriate, and then check the Cancel property before changing the CurrentItem and firing the CurrentChanged event.

Examples

The following code example demonstrates how to handle the CurrentChanging event. In this example, the XAML shows the content of a page with a GridView bound to a CollectionViewSource. The code-behind shows the CollectionViewSource initialization, which includes setting its Source and retrieving its View in order to attach the CurrentChanging event handler.

<Page.Resources>
  <CollectionViewSource x:Name="cvs" />
  <DataTemplate x:Key="myDataTemplate">
    <Border Background="#FF939598" Width="200" Height="200">
      <TextBlock Text="{Binding Path=Name}" />
    </Border>
  </DataTemplate>
</Page.Resources>

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
  <GridView x:Name="PicturesGrid" 
    SelectionMode="Single" CanReorderItems="False" CanDragItems="False"
    ItemsSource="{Binding Source={StaticResource cvs}}"                
    ItemTemplate="{StaticResource myDataTemplate}" >
    <GridView.ItemsPanel>
      <ItemsPanelTemplate>
        <WrapGrid VerticalChildrenAlignment="Top" 
          HorizontalChildrenAlignment="Left" />
      </ItemsPanelTemplate>
    </GridView.ItemsPanel>
  </GridView>
</Grid>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var library = Windows.Storage.KnownFolders.PicturesLibrary;
    var queryOptions = new Windows.Storage.Search.QueryOptions();
    queryOptions.FolderDepth = Windows.Storage.Search.FolderDepth.Deep;
    queryOptions.IndexerOption = 
        Windows.Storage.Search.IndexerOption.UseIndexerWhenAvailable;

    var fileQuery = library.CreateFileQueryWithOptions(queryOptions);

    var fif = new Windows.Storage.BulkAccess.FileInformationFactory(
        fileQuery, 
        Windows.Storage.FileProperties.ThumbnailMode.PicturesView, 190, 
        Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale, 
        false);

    var dataSource = fif.GetVirtualizedFilesVector();
    cvs.Source = dataSource;
    cvs.View.CurrentChanging += View_CurrentChanging;
}

private void View_CurrentChanging(object sender, CurrentChangingEventArgs e)
{
    Debug.WriteLine("Cancel = " + e.Cancel);
    Debug.WriteLine("IsCancelable = " + e.IsCancelable);
    if (e.IsCancelable == true)
    {
        // Cancel the change. The previously selected item remains selected.
        e.Cancel = true;
    }
}

See Also