4

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...

pixsperdavid
  • 147
  • 3
  • 14
  • Are you looking for Custom Dependency properties. http://msdn.microsoft.com/en-us/library/ms753358.aspx . – Jatin May 21 '13 at 10:35

1 Answers1

4

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.

Marc
  • 12,706
  • 7
  • 61
  • 97
  • Thanks Marc. Currently my control is defined in a ResourceDictionary in a XAML file which does not have an attached .cs file. Presumably I need to create one manually? – pixsperdavid May 21 '13 at 10:57
  • You mean you are defining a custom control template for the standard slider control? So there is no real custom control? – Marc May 21 '13 at 10:58
  • Yes it is a templated version of the standard control. Sorry if this wasn't clear. – pixsperdavid May 21 '13 at 11:03
  • Ok, no problem. Then I would suggest to derive a control from slider and define the properties there. That's the easiest way. Alternatively, you could use attached properties, I don't see any major differences in your case though. But maybe, you should go a different way and just define the brushes which you use as a resource and bind to them with a static resource binding... I'll update my answer... – Marc May 21 '13 at 11:08
  • Thanks, I'll look into sub-classing slider as it seems a neater solution. I'm coming at this from knowledge of QML where created a custom property for a UI control is much much easier... – pixsperdavid May 21 '13 at 11:18
  • True, it isn't actually easy, as everything with WPF... One trick which makes it a bit easier. If you start typing 'depend...', Visual studio will offer a shortcut in IntelliSense for creating the clumsy dependency property registration. – Marc May 21 '13 at 11:23