Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF/Silverlight : Templating Custom Control PARTIALLY

.

Hello guys,

Can we write our custom controls in such a way that later on we can re-template it partially? Means, without replacing the entire ControlTemplate, we want to change/override some of it's look and feel?

One way, of course, is this : copy the ControlTemplate code from generic.xaml, modify it's look and feel partially, and use it in your xaml, as shown below:

<MyCustomControl>
       <MyCustomControl.Template>
              <ControlTemplate>

                    <!-- paste the copied and modified code from generic.xaml here-->

              </ControlTemplate>
        </MyCustomControl.Template>

        <!--other code-->

</MyCustomControl>

But the problem with this approach is that, we make our xaml too verbose. And I seriously don't like it. I'm looking for some solution that may save me from polluting the xaml where I'm using my custom control.

Thanks in anticipation!

.

like image 753
Nawaz Avatar asked Dec 01 '10 08:12

Nawaz


1 Answers

I agree with most of what Martin says about ControlTemplates being pretty much an all or nothing thing in WPF and I was pretty frustrated myself when I came to this conclusion.

If your writing your own custom control however you can make it more flexible why composing or offering the ability to template pices of the control. For an example you can look at the newer WPF DataGrid control which has the ability to template different pieces of the control. Other headered content controls often have a separate template for the content and the header portion of the control making it possible to replace one but not the other.

like image 153
jpierson Avatar answered Sep 28 '22 04:09

jpierson