CollectionViewSource 類別

定義

提供一個資料來源,為收藏類別新增分組與當前項目支援。

public ref class CollectionViewSource sealed : DependencyObject
/// [Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 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.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class CollectionViewSource : DependencyObject
Public NotInheritable Class CollectionViewSource
Inherits DependencyObject
<CollectionViewSource .../>
繼承
Object Platform::Object IInspectable DependencyObject CollectionViewSource
屬性

範例

Tip

更多資訊、設計指導及程式碼範例,請參閱 語意縮放

WinUI 3 Gallery 應用程式包含大部分 WinUI 3 控制項、特性和功能的互動式範例。 從 Microsoft Store 取得應用程式,或在 GitHub 上取得原始程式碼。

以下程式碼範例示範如何將 ListBox 控制項綁定到分組 LINQ 查詢的結果。 在此範例中,一組隊伍依城市分組,並以城市名稱作為群組標題顯示。 完整程式碼列表請參閱 XAML 資料綁定範例

<Grid>

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

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

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

備註

當你想將清單控制項綁定到集合,但又想將這些集合分組顯示,並且維持一個獨立於清單控制項的當前項目時,可以使用 CollectionViewSource。 當你想綁定多個控制項到同一個集合,且想讓一個控制項中的當前項目改變另一個綁定控制項中的當前項目時,這特別有用。 你通常會將 CollectionViewSource 定義為 XAML 資源,並使用 {StaticResource} 標記擴充功能綁定。 接著你可以在程式碼背後設定它的 原始碼 屬性為支援的集合類型。

任何綁定到同一個 CollectionViewSource 的控制項,目前的項目都會是相同的。 你可以透過 CollectionViewSource.View 屬性值的 ICollectionView.CurrentItem 屬性,以程式方式存取目前的項目。

如果收藏中的項目本身是收藏,或是包含收藏的物件,你可以將收藏作為較大收藏中的群組顯示。 要做到這點,請將 IsSourceGrouped 屬性設 為 true。 如果這些項目包含收藏但本身不是集合,你也必須將 ItemsPath 屬性設定為集合屬性的名稱。

Note

不支援將 Source 屬性設定為另一個 CollectionViewSource 實例。

建構函式

名稱 Description
CollectionViewSource()

初始化 CollectionViewSource 類別的新實例。

屬性

名稱 Description
Dispatcher

它總是在 Windows 應用程式 SDK 應用程式中回傳 null 。 改用 DispatcherQueue

(繼承來源 DependencyObject)
DispatcherQueue

得到 DispatcherQueue 這個物件所關聯的那個。 代表 DispatcherQueue 一個功能,即使程式碼是由非 UI 執行緒發起,也能存取 UI DependencyObject 執行緒。

(繼承來源 DependencyObject)
IsSourceGrouped

取得或設定一個值,指示來源資料是否被分組。

IsSourceGroupedProperty

識別 IsSourceGrouped 相依屬性。

ItemsPath

取得或設定從頂層項目出發的屬性路徑,以尋找 CollectionViewSource 中的群組。

ItemsPathProperty

識別 ItemsPath 依賴屬性。

Source

取得或設定用於建立此視圖的集合物件。

SourceProperty

識別 來源 依賴性質。

View

取得目前與此 CollectionViewSource 實例相關聯的檢視物件。

ViewProperty

識別 View 依賴屬性。

方法

名稱 Description
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)

適用於

另請參閱