views:

77

answers:

1

I am looking for a good way to simplify and organise a complicated WPF custom control.

My first thought is to factor it out into two simpler controls. For the sake of explanation I'll call these SimpleControl and AdvancedControl.

SimpleControl contains all the core functionality and is extremely reusable.

AdvancedControl depends upon SimpleControl and contains input handling and extra bells and whistles. AdvancedControl is less reusable than SimpleControl because it contains extra UI elements and application specific input handling.

The visual template for AdvancedControl is what defines the dependence on SimpleControl. When OnApplyTemplate is called it caches a reference to the embedded SimpleControl.

The main problem with this approach is that all the properties and methods that are available on SimpleControl I also want to be available on AdvancedControl. This means a lot of work implementing the same dependency properties and methods on AdvancedControl and then just forwarding them through to SimpleControl. This doesn't seem to be particularly elegant.

I also thought about inheriting AdvancedControl from SimpleControl but I think in this case I would have to completely redefine the visual template for SimpleControl instead of just reusing the existing one.

Can anyone think of a better way of factoring out a complicated WPF custom control?

+1  A: 

You can inherit the style:

<Style TargetType="{x:Type AdvancedControl}" BasedOn="{x:Type SimpleControl}" />
CMerat
This is an interesting thing to learn but actually it doesn't really help me. It's not the style that I want to reuse in AdvancedControl. I want to reuse and extend the the visual template that is defined within the style. Is there any equivalent to 'BasedOn' that I can use with the visual template?
Ashley Davis
Perhaps putting the template in your resources and then referencing it with {StaticResource x}?
CMerat