ItemsControl.ItemsSource 属性


获取或设置用于生成 ItemsControl 的内容的集合。Gets or sets a collection used to generate the content of the ItemsControl.

 property System::Collections::IEnumerable ^ ItemsSource { System::Collections::IEnumerable ^ get(); void set(System::Collections::IEnumerable ^ value); };
public System.Collections.IEnumerable ItemsSource { get; set; }
member this.ItemsSource : System.Collections.IEnumerable with get, set
Public Property ItemsSource As IEnumerable


用于生成 ItemsControl 的内容的集合。A collection that is used to generate the content of the ItemsControl. 默认值为 nullThe default is null.



内容模型: 此属性可用于将项添加到 ItemsControlContent Model: This property may be used to add items to an ItemsControl.

常见的方案是使用 ListBoxListViewTreeViewItemsControl 来显示数据集合,或将 ItemsControl 绑定到集合对象。A common scenario is to use an ItemsControl such as a ListBox, ListView, or TreeView to display a data collection, or to bind an ItemsControl to a collection object. 若要将 ItemsControl 绑定到集合对象,请使用 ItemsSource 属性。To bind an ItemsControl to a collection object, use the ItemsSource property. 请注意,默认情况下 ItemsSource 属性支持 OneWay 绑定。Note that the ItemsSource property supports OneWay binding by default.

设置 ItemsSource 属性后,Items 集合将变为只读并固定大小。When the ItemsSource property is set, the Items collection is made read-only and fixed-size.

如果正在使用 ItemsSource,则将属性设置为 null 将删除该集合,并将使用情况还原到 Items,这将是一个空 ItemCollectionWhen ItemsSource is in use, setting the property to null removes the collection and restores usage to Items, which will be an empty ItemCollection. 当未使用 ItemsSource 时,此属性的值将 null,并将其设置为 null 不起作用。When ItemsSource is not in use, the value of this property is null, and setting it to null has no effect.


在大多数情况下,不需要实现自己的集合。In most cases you do not need to implement your own collections. 请考虑使用 ObservableCollection<T> 或其他现有集合。Instead, consider using ObservableCollection<T> or other existing collections. 有关详细信息,请参阅绑定源概述中的 "用作绑定源的集合对象"。For more information, see the "Collection Objects Used as Binding Source" in Binding Sources Overview.

XAML 特性用法XAML Attribute Usage

<object ItemsSource="bindingDeclaration"/>  


Binding 声明。A Binding declaration. 有关详细信息,请参阅绑定标记扩展See Binding Markup Extension for more information.

依赖项属性信息Dependency Property Information

标识符字段Identifier field ItemsSourceProperty
元数据属性设置为 trueMetadata properties set to true None