DataServiceCollection<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示在加入、移除項目或重新整理清單時提供通知的動態實體集合。
generic <typename T>
public ref class DataServiceCollection : System::Collections::ObjectModel::ObservableCollection<T>
public class DataServiceCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
type DataServiceCollection<'T> = class
inherit ObservableCollection<'T>
Public Class DataServiceCollection(Of T)
Inherits ObservableCollection(Of T)
類型參數
- T
任何實體類型。
- 繼承
範例
下列範例取自用於定義 WPF 中 SalesOrders
視窗之可延伸應用程式標記語言 (XAML) 頁面的程式碼後置頁面。 載入視窗時, DataServiceCollection<T> 會根據傳回具有相關物件的查詢結果,依國家/地區篩選,建立 。 此結果繫結至 DataContext (WPF 視窗的根配置控制項) 的 StackPanel 屬性。
using System;
using System.Data.Services.Client;
using System.Linq;
using System.Windows;
using NorthwindClient.Northwind;
namespace NorthwindClient
{
public partial class CustomerOrdersWpf : Window
{
private NorthwindEntities context;
private DataServiceCollection<Customer> trackedCustomers;
private const string customerCountry = "Germany";
private const string svcUri = "http://localhost:12345/Northwind.svc/";
public CustomerOrdersWpf()
{
//InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
// Initialize the context for the data service.
context = new NorthwindEntities(new Uri(svcUri));
// Create a LINQ query that returns customers with related orders.
var customerQuery = from cust in context.Customers.Expand("Orders")
where cust.Country == customerCountry
select cust;
// Create a new collection for binding based on the LINQ query.
trackedCustomers = new DataServiceCollection<Customer>(customerQuery);
// Bind the root StackPanel element to the collection;
// related object binding paths are defined in the XAML.
LayoutRoot.DataContext = trackedCustomers;
}
catch (DataServiceQueryException ex)
{
MessageBox.Show("The query could not be completed:\n" + ex.ToString());
}
catch (InvalidOperationException ex)
{
MessageBox.Show("The following error occurred:\n" + ex.ToString());
}
}
private void saveChangesButton_Click(object sender, RoutedEventArgs e)
{
// Save changes to the data service.
context.SaveChanges();
}
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Data.Services.Client
Imports NorthwindClient.Northwind
Partial Public Class CustomerOrdersWpf
Inherits Window
Private context As NorthwindEntities
Private trackedCustomers As DataServiceCollection(Of Customer)
Private Const customerCountry As String = "Germany"
Private Const svcUri As String = "http://localhost:12345/Northwind.svc/"
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
Try
' Initialize the context for the data service.
context = New NorthwindEntities(New Uri(svcUri))
' Create a LINQ query that returns customers with related orders.
Dim customerQuery = From cust In context.Customers.Expand("Orders") _
Where cust.Country = customerCountry _
Select cust
' Create a new collection for binding based on the LINQ query.
trackedCustomers = New DataServiceCollection(Of Customer)(customerQuery)
' Bind the root StackPanel element to the collection
' related object binding paths are defined in the XAML.
Me.LayoutRoot.DataContext = trackedCustomers
Catch ex As DataServiceQueryException
MessageBox.Show("The query could not be completed:\n" + ex.ToString())
Catch ex As InvalidOperationException
MessageBox.Show("The following error occurred:\n" + ex.ToString())
End Try
End Sub
Private Sub saveChangesButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Save changes to the data service.
context.SaveChanges()
End Sub
End Class
以下是上一個範例中用於定義 WPF 中 SalesOrders
視窗的 XAML。
<Window x:Class="CustomerOrdersWpf"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Height="423" Width="679" Loaded="Window_Loaded">
<StackPanel Orientation="Vertical" Height="Auto" Name="LayoutRoot" Width="Auto">
<Label Content="Customer ID" Margin="20,0,0,0" />
<ComboBox Name="customerIDComboBox" DisplayMemberPath="CustomerID" ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True" SelectedIndex="0" Height="23" Width="120"
HorizontalAlignment="Left" Margin="20,0,0,0" VerticalAlignment="Center" />
<ListView ItemsSource="{Binding Path=Orders}" Name="ordersDataGrid" Margin="34,46,34,50">
<ListView.View>
<GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Line Items">
<GridViewColumn DisplayMemberBinding="{Binding Path=OrderID, Mode=OneWay}"
Header="Order ID" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate, Mode=TwoWay}"
Header="Order Date" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Freight, Mode=TwoWay}"
Header="Freight Cost" Width="50"/>
</GridView>
</ListView.View>
</ListView>
<Button Name="saveChangesButton" Content="Save Changes" Click="saveChangesButton_Click"
Width="80" Height="30" Margin="450,0,0,0"/>
</StackPanel>
</Window>
備註
WCF Data Services 提供 類別,DataServiceCollection<T>以支援將數據系結至用戶端應用程式中的控件。 這個類別繼承自 ObservableCollection<T> 類別,它會實INotifyCollectionChanged作 介面,而且是 WPF) 和 Silverlight 型應用程式 Windows Presentation Foundation (的主要數據系結機制。
您可以透過使用實作 ObservableCollection<T> 介面的任何集合來載入 IEnumerable<T> 繫結集合。 載入到繫結集合的項目必須實作 INotifyPropertyChanged 介面。 如需詳細資訊,請參閱 將數據系結至控件。
建構函式
屬性
Continuation |
取得用於傳回下一組分頁式結果的接續物件。 |
Count |
取得 Collection<T> 中實際包含的項目數目。 (繼承來源 Collection<T>) |
Item[Int32] |
在指定的索引位置上取得或設定項目。 (繼承來源 Collection<T>) |
Items |
取得 IList<T> 周圍的 Collection<T> 包裝函式。 (繼承來源 Collection<T>) |
方法
事件
CollectionChanged |
發生於新增、移除或移動專案,或重新整理整個清單時。 (繼承來源 ObservableCollection<T>) |
PropertyChanged |
當屬性值變更時發生。 (繼承來源 ObservableCollection<T>) |
明確介面實作
ICollection.CopyTo(Array, Int32) |
從特定的 ICollection 索引開始,將 Array 的項目複製到 Array。 (繼承來源 Collection<T>) |
ICollection.IsSynchronized |
取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。 (繼承來源 Collection<T>) |
ICollection.SyncRoot |
取得可用以同步存取 ICollection 的物件。 (繼承來源 Collection<T>) |
ICollection<T>.IsReadOnly |
取得值,指出 ICollection<T> 是否唯讀。 (繼承來源 Collection<T>) |
IEnumerable.GetEnumerator() |
傳回逐一查看集合的列舉值。 (繼承來源 Collection<T>) |
IList.Add(Object) |
將項目加入至 IList。 (繼承來源 Collection<T>) |
IList.Contains(Object) |
判斷 IList 是否包含特定值。 (繼承來源 Collection<T>) |
IList.IndexOf(Object) |
判斷 IList 中指定項目的索引。 (繼承來源 Collection<T>) |
IList.Insert(Int32, Object) |
將項目插入 IList 中指定的索引處。 (繼承來源 Collection<T>) |
IList.IsFixedSize |
取得值,指出 IList 是否有固定的大小。 (繼承來源 Collection<T>) |
IList.IsReadOnly |
取得值,指出 IList 是否唯讀。 (繼承來源 Collection<T>) |
IList.Item[Int32] |
在指定的索引位置上取得或設定項目。 (繼承來源 Collection<T>) |
IList.Remove(Object) |
從 IList 移除特定物件之第一個符合的元素。 (繼承來源 Collection<T>) |
INotifyPropertyChanged.PropertyChanged |
當屬性值變更時發生。 (繼承來源 ObservableCollection<T>) |
擴充方法
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應