'宣言 Public Property DataSource As IEnumerable
public IEnumerable DataSource {get; set;}
| 例外 | 解説 |
|---|---|
| System.InvalidOperationException | アイテムを DataItems コレクション プロパティに直接追加した後、このプロパティを設定しようとしています。 |
| System.InvalidOperationException | IsExporting プロパティが true の場合にこのプロパティを設定しようとしています。 |
System.Collections.IEnumerable インターフェイスを実装する (文字列以外の) オブジェクトを DataSource として設定できます。このアーキテクチャは階層データと異なるデータ(ひとつのリスト内に複数のデータ型)の両方そしてツリーのようなより自由な形式の構造をサポートできます。現在いくつかの制限事項があります。たとえば GroupByArea は DefaultFieldLayout から FieldLayout.Fields しか表示しないため、ユーザーは異種データまたは子レコードからのデータの場合のように、複数タイプから Field ごとにグループ化できません。
DataRecord は遅延作成されて DataSource の各アイテムを表します。DataRecord は、データ ソースから関連付けられたアイテムを返す読み取り専用 DataRecord.DataItem プロパティおよび対応する DataRecord.DataItemIndex プロパティを公開します。これらの DataRecord は RecordManager によって管理され、RecordManager.Unsorted、RecordManager.Sorted および RecordManager.Groups コレクション プロパティを通して公開されます。
DataRecord が作成されると、FieldLayouts コレクションが既存の FieldLayout.Fields が DataRecord.DataItem プロパティと一致する FieldLayout に対して検索されます。見つからない場合、新しい FieldLayout が作成されます。その場合 FieldLayoutInitializing イベントおよび FieldLayoutInitialized イベントが発生します。新しい FieldLayout の FieldLayout.AutoGenerateFieldsResolved プロパティが true を返すと、データ項目のパブリック プロパティごとに FieldLayout.Fields コレクションに Field が自動的に生成されます。
注: このプロパティは DataItems プロパティと相互排他的で、このプロパティを設定し、DataItems コレクションに直接項目を追加しようと試みます。これによって InvalidOperationException がスローされます。また、NET Framework 4.5 で Microsoft がクロス スレッドの更新のサポートおよび BindingOperations の EnableCollectionSynchronization および AccessCollection メソッドによってアクセスするサポートを追加しました。DataPresenter コントロール ファミリは、DataSource プロパティが CollectionView に設定される場合にサポートします。たとえば:
var list = new ObservableCollection<MyClass>();
var lockingObj = new object();
// Call EnableCollectionSynchronization on the UI thread
BindingOperations.EnableCollectionSynchronization(list, lockingObj);
// Note: that XamDataGrid will only support cross-thread updating if the list is in a CollectionView
XamDataGrid1.DataSource = CollectionViewSource.GetDefaultView(list);
// perform updating of the collection on a background thread using locks
Task.Run(new Action(() =>
{
// lock using the same locking object that was passed into EnableCollectionSynchronization
// above on the UI thread
lock(lockingObj)
{
list.Add(new MyClass());
}
}));
// Alternatively, you can perform updating of the collection on a background thread using AccessCollection, e.g.:
Task.Run(new Action(() =>
{
// Since we will be calling AccessCollection on this thread we need to first call
// EnableCollectionSynchronization with the same locking object. This needs to be done
// on every thread that intends to call BindingOperations.AccessCollection
BindingOperations.EnableCollectionSynchronization(list, lockingObj);
// calling AccessCollection will wrap the action in a synchronization lock
BindingOperations.AccessCollection(list, new Action(() =>
{
list.Add(new MyClass());
}), true);
}));