How to add a property to a WPF user control and have it show up in the Properties panel of the Visual Studio designer

I had the need today to add my own property to a WPF user control, and have that property available in the Properties panel of the Visual Studio designer, so the property could be set at design-time. The purpose of this was that my user control had a toolbar, and I had come across the need to use the control, but not show the toolbar. Simple eh? Well, not quite!

My first thought was to add a normal C# property, but this didn’t show up in the Properties panel, so was quickly rejected.

I then came across this forum thread that showed how to add a Dependency property, which seemed to be what I needed. The property showed up in the Properties panel as required, but changing it didn’t affect the display of the toolbar.

It turns out that the static Dependency property cannot change properties of the non-static controls on the user control. You seem to need to call a non-static method to do that. I ended up with rather more code than I would have expected for such a simple task, but it looked like this…

public static readonly DependencyProperty ShowToolbarProperty =
  DependencyProperty.Register("ShowToolbar", typeof(bool), typeof(Cameras),
    new FrameworkPropertyMetadata(new PropertyChangedCallback(AdjustControl)));
public bool ShowToolbar {
  get {
    return (bool)GetValue(ShowToolbarProperty);
  set {
    SetValue(ShowToolbarProperty, value);
private static void AdjustControl(DependencyObject source, DependencyPropertyChangedEventArgs e) {
  (source as Cameras).UpdateControls((bool)e.NewValue);
private void UpdateControls(bool showToolbar) {
  CamerasToolbar.Visibility =
    (showToolbar ? Visibility.Visible : Visibility.Collapsed);

Lines 1 to 3 declare the dependency property itself, passing the name of the actual property that we want to show up in VS (in this case ShowToolbar), the type of that property, the type of the containing control, and a rather nasty-looking callback to a method that would be called whenever the property was changed.

Lines 5-12 are the actual property we want, and are a fairly normal C# property declaration, except that they use GetValue() and SetValue() on the dependency property.

Lines 14-16 set up the static method that is called when the dependency property is changed. As explained above, as this is static, it can’t change the properties of controls, so we need to grab hold of the object that raised the event (in this case, the parent control), cast it to the correct type, call a non-static method and pass in the new property value.

Lines 18-20 simply set the Visibility property of the CamerasToolbar as required.

Quite a lot of code for such a simple task eh?

Be First to Comment

Please note that all comments are manually approved before being displayed, so don't bother posting spam, I'll just delete it

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.