DataServiceCollection<T> 類別
表示在加入、移除項目或重新整理清單時提供通知的動態實體集合。
繼承階層
System.Object
System.Collections.ObjectModel.Collection<T>
System.Collections.ObjectModel.ObservableCollection<T>
System.Data.Services.Client.DataServiceCollection<T>
命名空間: System.Data.Services.Client
組件: Microsoft.Data.Services.Client (在 Microsoft.Data.Services.Client.dll 中)
語法
'宣告
Public Class DataServiceCollection(Of T) _
Inherits ObservableCollection(Of T)
'用途
Dim instance As DataServiceCollection(Of T)
public class DataServiceCollection<T> : ObservableCollection<T>
generic<typename T>
public ref class DataServiceCollection : public ObservableCollection<T>
type DataServiceCollection<'T> =
class
inherit ObservableCollection<'T>
end
JScript 不支援泛型型別及方法。
型別參數
- T
任何實體類型。
DataServiceCollection<T> 型別公開下列成員。
建構函式
上層
屬性
名稱 | 說明 | |
---|---|---|
Continuation | 取得用於傳回下一組分頁式結果的接續物件。 | |
Count | (繼承自 Collection<T>。) | |
Item | (繼承自 Collection<T>。) | |
Items | (繼承自 Collection<T>。) |
上層
方法
上層
事件
名稱 | 說明 | |
---|---|---|
CollectionChanged | (繼承自 ObservableCollection<T>。) | |
LoadCompleted | 在非同步載入作業完成時發生。 僅受到適用於 Silverlight 的 WCF Data Services 5.0 用戶端支援。 | |
PropertyChanged | (繼承自 ObservableCollection<T>。) |
上層
明確 繼承 實作
名稱 | 說明 | |
---|---|---|
IList.Add | (繼承自 Collection<T>。) | |
IList.Contains | (繼承自 Collection<T>。) | |
ICollection.CopyTo | (繼承自 Collection<T>。) | |
IEnumerable.GetEnumerator | (繼承自 Collection<T>。) | |
IList.IndexOf | (繼承自 Collection<T>。) | |
IList.Insert | (繼承自 Collection<T>。) | |
IList.IsFixedSize | (繼承自 Collection<T>。) | |
ICollection<T>.IsReadOnly | (繼承自 Collection<T>。) | |
IList.IsReadOnly | (繼承自 Collection<T>。) | |
ICollection.IsSynchronized | (繼承自 Collection<T>。) | |
IList.Item | (繼承自 Collection<T>。) | |
INotifyPropertyChanged.PropertyChanged | (繼承自 ObservableCollection<T>。) | |
IList.Remove | (繼承自 Collection<T>。) | |
ICollection.SyncRoot | (繼承自 Collection<T>。) |
上層
備註
WCF Data Services 提供 DataServiceCollection<T> 類別,以支援繫結資料到用戶端應用程式中的控制項。 此類別繼承自 ObservableCollection<T> 類別,該類別會實作 INotifyCollectionChanged 介面,並且是 Windows Presentation Foundation (WPF) 和 Silverlight 應用程式的主要資料繫結機制。
您可以透過使用實作 IEnumerable<T> 介面的任何集合來載入 ObservableCollection<T> 繫結集合。 載入到繫結集合的項目必須實作 INotifyPropertyChanged 介面。 如需詳細資訊,請參閱將資料繫結至控制項 (WCF Data Services)。
範例
下列範例取自用於定義 WPF 中 SalesOrders 視窗之可延伸應用程式標記語言 (XAML) 頁面的程式碼後置頁面。 載入視窗時,將會根據傳回客戶及相關物件 (依國家 (地區) 篩選) 的查詢結果建立 DataServiceCollection<T>。 此結果繫結至 StackPanel (WPF 視窗的根配置控制項) 的 DataContext 屬性。
Imports System
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 = "https://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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Services.Client;
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 = "https://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();
}
}
}
以下是上一個範例中用於定義 WPF 中 SalesOrders 視窗的 XAML。
<Window x:Class="CustomerOrdersWpf"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="https://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>
執行緒安全性
這個型別的任何公用 static (在 Visual Basic 中為 Shared) 成員都是執行緒安全的。並不是所有的執行個體成員都保證可以用於所有的執行緒。
請參閱
參考
System.Data.Services.Client 命名空間