WPF DataContext 与 ItemsSource 性能
假设我们有一个 ItemsControl,它绑定到一个源。 之间是否有任何性能差异
ItemsControl.DataContext = resultSet;
和
ItemsControl.ItemsSource = resultSet;
(在两种情况下都在 XAML 中正确绑定)
Suppose we have an ItemsControl, which is bounded to a source.
Is there any performance difference between
ItemsControl.DataContext = resultSet;
and
ItemsControl.ItemsSource = resultSet;
(In both cases correctly binded in XAML)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,性能差异并不重要,因为两条线路执行完全不同的操作。 DataContext 是 ItemsControl 的本地数据绑定所取自的对象:
将采用设置为 DataContext 的对象的 Length 属性并将其绑定到 ItemsControl 的 Width 依赖属性。
另一方面,ItemSource 是 IEnumerable 对象,应该对其进行迭代以在控件内创建子项。 (ItemSource 内的每个对象都将成为它创建的子项的 DataContext)
Well, a performance difference doesn't really matter since the two lines do completely different things. The DataContext is the object that local databindings of the ItemsControl are taken from:
Will take the Length property of the object set as the DataContext and bind it to the Width dependency property of the ItemsControl.
On the other hand the ItemSource is the IEnumerable object that should be iterated to create the child items inside the control. (Each object inside the ItemSource will become the DataContext of the child item it created)