[英]What's the difference between a WPF DataGrid's Items and ItemsSource properties?
From what I can gather from Intellisense, the difference is the return types and the ItemsSource has a setter whereas Items simply has a getter.从我可以从 Intellisense 收集到的信息来看,不同之处在于返回类型,ItemsSource 有一个 setter,而 Items 只有一个 getter。 Practically speaking though, I do not understand the need for these two properties being separate from each other instead of just being one property.
但实际上,我不明白需要将这两个属性彼此分开而不仅仅是一个属性。
Could someone explain to me why these are separate properties instead of just one property?有人可以向我解释为什么这些是单独的属性而不是一个属性吗?
And also, if I'm missing something, could someone please explain to me when I'd want to use one over the other (besides the obvious need of a setter)?而且,如果我遗漏了什么,有人可以向我解释什么时候我想使用一个而不是另一个(除了明显需要一个二传手)? Eg, when specifically would I want to use Items over ItemsSource?
例如,我什么时候特别想在 ItemsSource 上使用 Items?
What's the difference between a WPF DataGrid's Items and ItemsSource properties?
WPF DataGrid 的 Items 和 ItemsSource 属性之间有什么区别?
A DataGrid is an ItemsControl so this applies to all other ItemsControl classes as well. DataGrid 是一个 ItemsControl,因此这也适用于所有其他 ItemsControl 类。
The Items property is an ItemCollection and is filled in through XAML. Items 属性是一个 ItemCollection 并通过 XAML 填充。 It holds objects but is intended for FrameworkElements.
它包含对象,但用于 FrameworkElements。
The ItemsSource is bindable to a simple IEnumerable, with the ability to support INotifyCollectionChanged when available. ItemsSource 可绑定到一个简单的 IEnumerable,并能够在可用时支持 INotifyCollectionChanged。 It also supports DataTemplates.
它还支持数据模板。
when specifically would I want to use Items over ItemsSource?
我什么时候特别想在 ItemsSource 上使用 Items?
ItemsSource is for databinding to a ViewModel. ItemsSource 用于数据绑定到 ViewModel。
You would use Items only in a few situations where you have a fixed number of XAML items.仅在具有固定数量 XAML 项目的少数情况下才使用 Items。 Unlikely for a Grid, more usable for a ComboBox.
对于 Grid 不太可能,对于 ComboBox 更有用。
You never use both at the same time.你永远不会同时使用两者。
This MSDN Page shows the typical usage for both.此MSDN 页面显示了两者的典型用法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.