CollectionViewSource クラス

定義

コレクション クラスにグループ化と現在の項目のサポートを追加するデータ ソースを提供します。

public ref class CollectionViewSource sealed : DependencyObject
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class CollectionViewSource final : DependencyObject
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class CollectionViewSource final : DependencyObject
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class CollectionViewSource : DependencyObject
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class CollectionViewSource : DependencyObject
Public NotInheritable Class CollectionViewSource
Inherits DependencyObject
<CollectionViewSource .../>
継承
Object Platform::Object IInspectable DependencyObject CollectionViewSource
属性

Windows の要件

デバイス ファミリ
Windows 10 (10.0.10240.0 で導入)
API contract
Windows.Foundation.UniversalApiContract (v1.0 で導入)

ヒント

詳細、設計ガイダンス、およびコード例については、「 データ バインディングの詳細」を参照してください。

WinUI 2 ギャラリー アプリがインストールされている場合は、ここをクリックしてアプリを開き、SemanticZoom を使用したグループ化の例を確認します

次のコード例では、 ListBox コントロールをグループ化された LINQ クエリの結果にバインドする方法を示します (チームのコレクションは都市ごとにグループ化され、市区町村名がグループ ヘッダーとして表示されます)。 完全なコード一覧については、 XAML データ バインディングのサンプルを参照してください。

<Grid>

  <Grid.Resources>
    <CollectionViewSource x:Name="groupInfoCVS" IsSourceGrouped="true"/>
  </Grid.Resources>

  <ListBox x:Name="lbGroupInfoCVS" 
    ItemsSource="{Binding Source={StaticResource groupInfoCVS}}">

    <ListBox.GroupStyle>
      <GroupStyle>
        <GroupStyle.HeaderTemplate>
          <DataTemplate>

            <TextBlock Text="{Binding Key}"/>

          </DataTemplate>
        </GroupStyle.HeaderTemplate>
      </GroupStyle>
    </ListBox.GroupStyle>

    <ListBox.ItemTemplate>
      <DataTemplate>

        <Border Background="{Binding Color}" 
          Width="200" CornerRadius="10" HorizontalAlignment="Left">

          <TextBlock Text="{Binding Name}" 
            Style="{StaticResource DescriptionTextStyle}" 
            HorizontalAlignment="Center" FontWeight="Bold"/>

        </Border>
      </DataTemplate>
    </ListBox.ItemTemplate>

  </ListBox>

</Grid>
Teams teams = new Teams();
var result = 
    from t in teams 
    group t by t.City into g 
    orderby g.Key 
    select g;
groupInfoCVS.Source = result;

詳細な例については、 XAML データ バインディングのサンプルを参照してください。

注釈

CollectionViewSource は、リスト コントロールをコレクションにバインドする場合に、それらのコレクションをグループに表示し、リスト コントロールとは独立した現在の項目を維持する場合に使用します。 これは、複数のコントロールを同じコレクションにバインドし、あるコントロールの現在の項目で他の連結コントロールの現在の項目を変更する場合に特に便利です。 通常、CollectionViewSource を XAML リソースとして定義し、 {StaticResource} マークアップ拡張機能を使用してバインドします。 その後、分離コードの Source プロパティをサポートされているコレクション型に設定できます。

同じ CollectionViewSource にバインドするすべてのコントロールは、常に同じ現在の項目を持つことになります。 CollectionViewSource.View プロパティ値の ICollectionView.CurrentItem プロパティを使用して、現在の項目にプログラムでアクセスできます。

コレクション内の項目がコレクション自体であるか、コレクションを含むオブジェクトである場合は、大きなコレクション内のグループとしてコレクションを表示できます。 これを行うには、 IsSourceGrouped プロパティを true に設定 します。 項目にコレクションが含まれているが、コレクション自体ではない場合は、 ItemsPath プロパティをコレクション プロパティの名前に設定する必要もあります。

注意

Source プロパティを別の CollectionViewSource インスタンスに設定することはサポートされていません。

コンストラクター

CollectionViewSource()

CollectionViewSource クラスの新しいインスタンスを初期化します。

プロパティ

Dispatcher

このオブジェクトが関連付けられている CoreDispatcher を取得します。 CoreDispatcher は、コードが UI 以外のスレッドによって開始された場合でも、UI スレッド上の DependencyObject にアクセスできる機能を表します。

(継承元 DependencyObject)
IsSourceGrouped

ソース データをグループ化するかどうかを示す値を取得または設定します。

IsSourceGroupedProperty

IsSourceGrouped 依存関係プロパティを識別します。

ItemsPath

CollectionViewSource 内のグループを検索するための最上位項目から従うプロパティ パスを取得または設定します。

ItemsPathProperty

ItemsPath 依存関係プロパティを識別します。

Source

このビューの作成元のコレクション オブジェクトを取得または設定します。

SourceProperty

Source 依存関係プロパティを識別 します

View

CollectionViewSource のこのインスタンスに現在関連付けられているビュー オブジェクトを取得します。

ViewProperty

View 依存関係プロパティを識別します。

メソッド

ClearValue(DependencyProperty)

依存関係プロパティのローカル値をクリアします。

(継承元 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

依存関係プロパティに対して確立された基本値を返します。これは、アニメーションがアクティブでない場合に適用されます。

(継承元 DependencyObject)
GetValue(DependencyProperty)

DependencyObject から依存関係プロパティの現在の有効な値を返します。

(継承元 DependencyObject)
ReadLocalValue(DependencyProperty)

ローカル値が設定されている場合は、依存関係プロパティのローカル値を返します。

(継承元 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

この DependencyObject インスタンスの特定の DependencyProperty に対する変更をリッスンするための通知関数を登録します。

(継承元 DependencyObject)
SetValue(DependencyProperty, Object)

DependencyObject の依存関係プロパティのローカル値を設定します。

(継承元 DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

RegisterPropertyChangedCallback を呼び出して以前に登録した変更通知を取り消します。

(継承元 DependencyObject)

適用対象

こちらもご覧ください