Delen via


ICollectionView.CurrentChanging Event

Definition

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

// Register
event_token CurrentChanging(CurrentChangingEventHandler const& handler) const;

// Revoke with event_token
void CurrentChanging(event_token const* cookie) const;

// Revoke with event_revoker
ICollectionView::CurrentChanging_revoker CurrentChanging(auto_revoke_t, CurrentChangingEventHandler const& handler) const;
event CurrentChangingEventHandler CurrentChanging;
function onCurrentChanging(eventArgs) { /* Your code */ }
iCollectionView.addEventListener("currentchanging", onCurrentChanging);
iCollectionView.removeEventListener("currentchanging", onCurrentChanging);
- or -
iCollectionView.oncurrentchanging = onCurrentChanging;
Event CurrentChanging As CurrentChangingEventHandler 

Event Type

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;
    }
}

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.

Applies to