WPF DataContext 与 ItemsSource 性能

发布于 2024-08-02 16:41:32 字数 217 浏览 5 评论 0原文

假设我们有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2024-08-09 16:41:32

好吧,性能差异并不重要,因为两条线路执行完全不同的操作。 DataContext 是 ItemsControl 的本地数据绑定所取自的对象:

<ItemsControl Width={Binding Length} />

将采用设置为 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:

<ItemsControl Width={Binding Length} />

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文