views:

111

answers:

3

Hi,

I have several similar user controls which display listviews of respectively different data entities. In the code-behind files of these controls there is quite a bit of common business logic.

How can these common methods be combined in a single "super-class" as I would do in a normal non-wpf-case (reusing any xaml parts isnt necessary)? Creating a super-control (with just a skeleton xaml file) which the controls inherit from gives a parser error of:

Partial declarations of DERIVED-CLASS must not specify different base classes

in the derived user controls.

Thank you a lot.

+1  A: 

Sounds like DataTemplating is what you want. You set up a general custom list view that contains all your business logic and then use data templating in the XAML to customise the view depending on the type of object that it contains.

Here is a great link to get you started: http://msdn.microsoft.com/en-us/library/ms742521.aspx

Alastair Pitts
thank you - while Pauls answer above solved my (actually quzite stupid) immedeate problem I will certainly look to rething the overal approach as you suggested
Hinnerk Brügmann
+1  A: 

How did you use the "super-control" in the derived user controls? Simply change the base class?

If so, you need to change the root element of your XAML file (i.e. the one on which x:Class is specified) to match.

Paul Baker
+1  A: 

My best suggestion is to make lookless controls (controls w/ no XAML associated). These you can inherit and ultimately more powerful because the styles you can make will be more flexible (ie Button you can style into anything. A button is a lookless control). Your default style for the control can be placed in the Themes\generic.xaml, and you can make other default styles for subclassed controls. These styles can have copies that exist in other resource dicts also.

I cover lookless controls here on my blog post, but it is geared towards silverlight. It's slightly different syntactically in WPF, but same ideas.

Jeremiah Morrill