0

I have built a UserControl that is basically a grid that can have either rounded corners on each end, or a certain polygon. I have a Rounded property that changes the Visibility of the rounded border and the polygon to match (if someone sets Rounded="True", then the rounded border is visible and the polygon is hidden, and vice-versa.

Just like in this question: UserControl Dependency Property design time

...it works great at run-time, but I can't seem to get it to reflect the changes at design-time. However, restarting VS, cleaning the solution, rebuilding, changing the build target, etc - none of those steps seem to make a difference. My class is pretty basic:

public partial class MyBox : UserControl
{
    public MyBox()
    {
        InitializeComponent();
    }

    public bool Rounded
    {
        get { return (bool)GetValue(RoundedProperty); }
        set
        {
            SetValue(RoundedProperty, value);
            this.edgeRounded.Visibility = (value ? Visibility.Visible : Visibility.Hidden);
            this.edgePolygon.Visibility = (value ? Visibility.Hidden : Visibility.Visible);
        }
    }

    public static readonly DependencyProperty RoundedProperty = DependencyProperty.Register("Rounded", typeof(bool), typeof(MyBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
}

Any ideas?

jhilgeman
  • 1,543
  • 10
  • 27
  • I got this to work by adding a changed event handler and then changing the dependency property registration to reference it: public static readonly DependencyProperty RoundedProperty = DependencyProperty.Register("Rounded", typeof(bool), typeof(MyBox), new PropertyMetadata(false, RoundedChanged)); – jhilgeman Jan 18 '19 at 06:17

2 Answers2

3

I got this to work by adding a changed event handler and then changing the dependency property registration to reference it. My working code:

public partial class MyBox : UserControl
{
    public MyBox()
    {
        InitializeComponent();
    }

    public bool Rounded
    {
        get { return (bool)GetValue(RoundedProperty); }
        set { SetValue(RoundedProperty, value); }
    }

    public static readonly DependencyProperty RoundedProperty = DependencyProperty.Register("Rounded", typeof(bool), typeof(MyBox), new PropertyMetadata(false, RoundedChanged));

    private static void RoundedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        bool value = (bool)e.NewValue;
        MyBox thisMyBox = (MyBox)sender;

        // Hide/show the edges
        thisMyBox.edgeRounded.Visibility = (value ? Visibility.Visible : Visibility.Hidden);
        thisMyBox.edgePolygon.Visibility = (value ? Visibility.Hidden : Visibility.Visible);
    }
}
jhilgeman
  • 1,543
  • 10
  • 27
0

Maybe use the XAML to bind the visibility property to a datacontext viewmodel. I have done this with all kinds of things.

Slipoch
  • 750
  • 10
  • 23
  • The goal is to have design-time properties, though, so I can have: and see the edges change based on the design property. – jhilgeman Jan 18 '19 at 05:38
  • The model above could inherit from the INotifyPropertyChanged as well as the UserControl so that you just implement the propertychanged event rather than having the dependencyproperty. – Slipoch Jan 20 '19 at 04:50