[ACCEPTED]-ControlTemplate for existing controls in WPF-controltemplate
Check out StyleSnooper:
It will dump out the standard 4 styles (and therefore templates too) for 3 the built in controls. You can also load 2 in a specific DLL that contains WPF controls 1 and view the default styles for those too.
The styles along with template examples 3 are up on MSDN for download, see the Default WPF Themes
link.
However you 2 can also extend the existing style without 1 redefining everything by using the BasedOn
attribute.
If you have Expression Blend you can:
- Drag the control onto the design surface
- Right click the control and choose Edit Template -> Edit Copy
When you do this, Blend 5 will extract the base template from the 4 control and explicitly declare it within 3 document/application as a resource which 2 you can then edit to your liking. You can 1 do this for any control.
The book "Pro WPF in C# 2008", by Matthew 4 MacDonald, includes a Control Template browser 3 in Chapter 15. I believe you can simply 2 download the sample code from the Apress 1 web site.
2020 Update:
All the styles and templates of WPF components 17 are now moved here.
But, before creating a new 16 component, just check this to see if you have 15 a good alternative to that (maybe changing 14 the style can work).
Note: Usually, it has a 13 bunch of DynamicResource bindings that you 12 might want to replace with yours and make 11 static. If you don't want to do much manual 10 work, you might consider using the second 9 solution below.
The second and shorter solution 8 might be using Microsoft Blend to extract the template 7 with the following steps:
Right-click on 6 the control >
Edit Template
>Edit Current
OREdit a Copy
But be careful with 5 this one, as it doesn't always export the 4 whole template,
but the necessary part of 3 it.
Just consider comparing this template 2 with the official one (mentioned above) to 1 make sure everything is fine.
Use Microsoft Blend for it: Paste your entire 4 XAML code in a file in this tool and right 3 click the control whose visual tree you 2 want to perceive:
Select the option: Edit 1 template and there you go
The XamlWriter class provides you with this 5 functionality. If controlName
is the Name of a Control
then 4 using the below snippet you get the Xaml 3 of the Control's Template inside the stringBuilder
object.
I 2 guess tools mentioned in the answers utilize 1 this class.
var stringBuilder = new StringBuilder();
var xmlSettings = new XmlWriterSettings
{
Indent = true
};
using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
{
XamlWriter.Save(controlName.Template, xmlWriter);
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.