Share via


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
屬性

範例

提示

如需詳細資訊、設計指引和程式碼範例,請參閱 語意式縮放

WinUI 3 資源庫應用程式包含大部分 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}}">

    <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} 標記延伸系結至它。 然後,您可以在程式代碼後置中將其 Source 屬性設定為支援的集合類型。

系結至相同 CollectionViewSource 的任何控件一律會有相同的目前專案。 您可以透過 CollectionViewSource.View 屬性值的 ICollectionView.CurrentItem 屬性,以程式設計方式存取目前的專案。

如果集合中的專案本身是集合,或是包含集合的物件,您可以將集合顯示為較大集合內的群組。 若要這樣做,請將 IsSourceGrouped 屬性設定為 true。 如果專案包含集合,但不是集合本身,您也必須將 ItemsPath 屬性設定為集合屬性的名稱。

注意

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

建構函式

CollectionViewSource()

初始化 CollectionViewSource 類別的新實例。

屬性

Dispatcher

一律會在 Windows 應用程式 SDK 應用程式中傳null回 。 請改用 DispatcherQueue

(繼承來源 DependencyObject)
DispatcherQueue

DispatcherQueue取得與這個 物件相關聯的 。 DispatcherQueue表示即使程式代碼是由非UI線程起始,也可以存取 DependencyObject UI線程上的設備。

(繼承來源 DependencyObject)
IsSourceGrouped

取得或設定值,這個值表示源數據是否已分組。

IsSourceGroupedProperty

識別 IsSourceGrouped 相依性屬性。

ItemsPath

取得或設定要從最上層項目追蹤的屬性路徑,以尋找 CollectionViewSource 內的群組。

ItemsPathProperty

識別 ItemsPath 相依性屬性。

Source

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

SourceProperty

識別 來源 相依性屬性。

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)

適用於

另請參閱