Share via


ICollectionView.CurrentChanging 事件

定義

實作此介面時,請在變更目前專案之前引發此事件。 事件處理常式可以取消此事件。

// 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 

事件類型

範例

下列程式碼範例示範如何處理 CurrentChanging 事件。 在此範例中,XAML 會顯示系結至CollectionViewSourceGridView頁面內容。 程式碼後置會顯示 CollectionViewSource 初始化,其中包括設定其 Source 並擷取其 View ,以附加 CurrentChanging 事件處理常式。

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

備註

CurrentItem 屬性值變更時,就會發生 CurrentChanging 事件。 傳遞至事件處理常式的 CurrentChangingEventArgs 參數會指定變更的相關資訊。 如果 IsCancelabletrue,事件處理常式可以藉由將 Cancel 設定為 true來取消變更。 如果變更已取消, 則 CurrentItem 不會變更。 當IsCancelablefalse時,將Cancel設定為true會擲回例外狀況。

實作此介面的類別應該會引發 CurrentChanging 事件、視需要設定IsCancelable,然後在變更CurrentItem並引發CurrentChanged事件之前檢查Cancel屬性。

適用於