开发者

WPF: How to reuse controls when ObservableCollection is concrete?

开发者 https://www.devze.com 2023-01-17 01:16 出处:网络
I have a object that inherits from TabItem. I have a bunch of Database objects that will reuse the same code so I wanted only one TabItem class and then use DataTemplates to control how each object ge

I have a object that inherits from TabItem. I have a bunch of Database objects that will reuse the same code so I wanted only one TabItem class and then use DataTemplates to control how each object gets presented.

Problem is that the TabItem shows a collection of Objects and ObservableCollection is concrete.

I've pondered a few solutions but none o开发者_运维百科f them work. Seems like I will have to create one class for each object type even when they will all be the same (except for ObservableCollection having different types). That's not very DRY.

I can't make a UserControl generic, I can't let the UserControl constructor take in a generic class ( unless I define the Type wihc I don't wan't to do). I guess creating a base UserControl class and then inheriting that will have to do. Does it inherit the XAML code as well or will I have to rely on styles and templates?

Am I missing something?


Look into using DataTemplateSelector to provide flexibily in how you present your data in WPF.

Here are three sites that helped me:

  • Data Templating Overview

  • How to use DataTemplateSelector to alter views of objects in a ListBox

  • WPF Tutorial - How To Use A DataTemplateSelector

0

精彩评论

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