WPF / Silverlight: optional PARTIALLY template management

.

Hi guys,

Can we write our custom controls so that we can later partially re-dock it? So, without replacing the entire ControlTemplate , we want to change / override some of its appearance?

Of course, one way: copy the ControlTemplate code from generic.xaml, partially change its appearance and feel 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 really don't like it. I am looking for some solution that can save me from polluting the xaml environment, where I use my user control.

Thanks pending!

.

+6
wpf silverlight xaml custom-controls controltemplate
source share
3 answers

I agree with most of what Martin says that ControlTemplates is almost all or nothing in WPF, and I was very disappointed when I came to this conclusion.

If you are writing your own custom control, you can make it more flexible to create or propose a control template. As an example, you can see the new WPF DataGrid control, which has the ability to template various control elements. Other content header controls often have a separate template for the content and part of the control's header, which allows you to replace one, but not the other.

+1
source share

Unfortunately, changing the management template is all or nothing. As you point out, XAML becomes very verbose when you change a complex control pattern.

One approach (which is quite obvious, one way or another) is to create your user interface from small parts, hoping to make it easier to work with control templates.

You have the option of reusing styles using the Style.BasedOn Property . Unfortunately, this will not solve your problem.

+1
source share

I also face the same problem when creating a little editing requires copying and pasting the entire default control template.

However, you can define multiple resource dictionaries and use <ResourceDictionary.MergedDictionaries> to separate your markup from control patterns.

Your xaml markup will be much cleaner and will look something like this:

  <Style> <Setter Property="Template" Value="{StaticResource SomeRandomTemplate}" /> </Style> 
+1
source share

All Articles