简体   繁体   English

WPF DataGrid 的 Items 和 ItemsSource 属性之间有什么区别?

[英]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.

相关问题 WPF DataGrid 的 EnableRowVirtualization 和 VirtualizingStackPanel.IsVirtualizing 属性之间的区别 - Difference between WPF DataGrid's EnableRowVirtualization and VirtualizingStackPanel.IsVirtualizing properties 绑定到Datagrid的ItemsSource集合属性,而不是单个项目 - Binding to Datagrid's ItemsSource collection properties instead of single items 如何将DataGrid的ItemsSource拆分为多个DataGrid WPF? - How to split datagrid's itemssource into multiple datagrids wpf? WPF 使用数据/设置 ItemsSource 填充 Datagrid 的 DataGridCheckBoxColumn - WPF Populating Datagrid's DataGridCheckBoxColumn with data / setting ItemsSource 创建一个自定义DataGrid的ItemsSource - Create a custom DataGrid's ItemsSource WPF DataGrid绑定到ItemsSource项目属性 - WPF DataGrid bind to ItemsSource items property WPF DataGrid - 从 DataGrid ItemsSource 对象的集合值中设置每行(对象)唯一的 combobox 值 - WPF DataGrid - Set unique per row (object) combobox values from the DataGrid ItemsSource object's collection value 在WPF中,RenderTransform的null和Identity之间有什么区别? - In WPF, what's the difference between null and Identity for RenderTransform? WPF / UWP:DependencyObject的GetValue()和ReadLocalValue()之间有什么区别? - WPF/UWP: What is the difference between DependencyObject's GetValue() and ReadLocalValue()? WPF Dispatcher的InvokeAsync和BeginInvoke之间有什么区别 - What's the difference between InvokeAsync and BeginInvoke for WPF Dispatcher
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM