Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending user controls in WPF

I have built a User Control composed, as usual, of a XAML part and a code-behind part. Now, I need to create another User Control which shares some of the functionalities of the former, but which has different looks and might also have additional features in the code-behind.

My first idea was to create an interface to gather the common functionalities of the two types of controls. Is this the right way to go? And how would I manage the different XAML parts I should have? Any advice is welcome.

like image 744
Tilvia Avatar asked Nov 20 '11 16:11

Tilvia


3 Answers

I would like to add another contribution which I think might be useful to others encountering my same situation.

One other possible solution, which in my opinion is suitable if the controls you are going to create are not particularly complex, is to create a base User Control containing the common features that you want to share: such control will be written entirely in C#. This, in fact, allows inheritance in User Controls composed of both XAML and code behind. In the XAML of the inherited control, rather than having

<UserControl> ... </UserControl>

You will have

<MyProject: MyBaseControl x:Class="MyProject.MyExtendedControl"> ... </MyProject: MyBaseControl>

and then in the code behind you will also need to specify the following:

class MyExtendedControl : MyBaseControl

Hope this helps.

like image 179
Tilvia Avatar answered Nov 04 '22 15:11

Tilvia


User controls don't lend themselves well to inheritance in WPF, you can do some hacks to make it work, but overall you'll probably be better off using a Custom Control instead. It's easier to specify different XAML for inherited controls using this technique. The easiest way to get started with a custom control is to add a new item to your project of type "Custom Control (WPF)" and it will add the control and the XAML gets added to Themes/generic.xaml for you.

To inherit from this control, the easiest way is to add another new Custom Control (WPF) and then change the code behind to inherit from your parent control instead of Control.

like image 2
Bill Reiss Avatar answered Nov 04 '22 17:11

Bill Reiss


There are a number of ways to split up the pieces to make them easier to work with.

MVVM, as mentioned in a comment, relies upon Data Binding to separate the input controls from logic. By setting the appropriate class which implements INotifyPropertyChanged into the DataContext of you control, you can change the behaviour. Your DataContext class or ViewModel could implement Visibility properties to show and hide different parts of the input if the differences are not too great between the uses.

Another way to break apart functionality is by Aggregation. See StackOverflow 269496

You could build smaller controls that implement common functionality and then build larger special purpose controls by combining the smaller controls (i.e. aggregating them) to make the larger control. Aggregation will work for both the code-behind and the Data Binding approaches.

like image 2
Doug Ferguson Avatar answered Nov 04 '22 15:11

Doug Ferguson