开发者

UserControl instead of DataTemplates - is there a way for big collections?

开发者 https://www.devze.com 2023-03-15 07:53 出处:网络
There are two ways. May be both are stupid... I have to display some collections of items. First one. I use DataTemplate for ListBoxItem.

There are two ways. May be both are stupid... I have to display some collections of items.

First one.

I use DataTemplate for ListBoxItem. Just set itemSource = myCollection;

That开发者_StackOverflow社区's all. Simple scheme.

Second one.

Each item in my collection has property view. It's a UserControl. That define how item renders.

Create DataTemplate with ContentPresenter only.

Binding Content property to a view.

Just set itemSource = myCollection;

That's all. More complex. But works too.

Has second one right to live? My doubt is that I have to create instance of UserControl for every item in my collection?

Is not it too expensive for collection with over than 500 items?

Thanks.


I don't believe there is much difference, with the DataTemplate approach the framework will create an instance of the DataTemplate for each item in the collection. In the second approach an instance of the user control will be created for each item, there may be a few more controls but only a few per item.

One reason the second approach could be preferable is that you can have logic around which content is bound. This could mean different user controls for each item in the list. Caliburn Micro lets you use this approach very naturally.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号