简体   繁体   中英

WPF Control Templating - How do I create a custom property?

I'm currently building a XAML UserControl library using Blend.

I've created a template for my custom WPF slider type. What I'd like to be able to do now is add a custom brush property for the control which will allow easy customisation when designing the application interface (my slider design has a background colour, foreground colour and a thumbnail colour). Is this possible, and if so, will it appear in the Blend interface?

I know I could just create a load of styles, but it seems overkill for just changing a single colour...

In the codebehind of your UserControl, define dependency properties for the brushes you want to use in your template.

public static readonly DependencyProperty ThumbnailBrushProperty =
            DependencyProperty.Register("Command", typeof (Brush), typeof (YourControl), new PropertyMetadata(default(Brush)));

This property will show up in the VS designer and Blend in the Miscellaneous section.

If I understand you right, you have created a ControlTemplate for your custom slider control. If so, you can use the property from within the template by binding to it via a template binding:

UPDATE

Ok, so your creating a template for the standard slider control. Have you considered defining the brushes which you use as ThumbnailBrush etc. in a resource, reference this resource dictionary in the resource dictionary which holds your slider template and just use them from within the template? In Blend you would than be able to modify it in the Resources tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM