简体   繁体   中英

DependencyProperty causes Visual Studio to crash during design time

In Visual Studio, when attempting to view the properties of my custom control via design mode, Visual Studio crashes before I can see the properties.

This change has started ever since implementing dependency properties into my control.

Why does this happen (or what causes this)? What is required for the dependency properties to be set up correctly?

NOTE: The custom control is in a separate .DLL, and I cannot configure Visual Studio to display my custom control in the toolbox. However, Visual Studio knows the control exists, because it appears as a suggestion when typing XAML manually.

Also note that I am able to view the default Common Properties. This problem occurs when I expand the Other Properties.

Below are the dependency properties that I have set up:

public static readonly DependencyProperty ThicknessProperty =
    DependencyProperty.Register("Thickness", typeof(double), typeof(LineTypeViewer), new FrameworkPropertyMetadata(1, OnPropertyChanged));

public static readonly DependencyProperty ForegroundProperty =
    DependencyProperty.Register("Foreground", typeof(SolidColorBrush), typeof(LineTypeViewer), new FrameworkPropertyMetadata(Brushes.Black, OnPropertyChanged));

public static readonly DependencyProperty CurrentLineTypeProperty =
    DependencyProperty.Register("CurrentLineType", typeof(LineType), typeof(LineTypeViewer), new FrameworkPropertyMetadata(null, OnPropertyChanged));

public static readonly DependencyProperty DrawingWidthProperty =
    DependencyProperty.Register("DrawingWidth", typeof(float), typeof(LineTypeViewer), new FrameworkPropertyMetadata(5, OnPropertyChanged, OnCoerceDrawingWidthProperty));

public static readonly DependencyProperty MinRepetitionsProperty =
    DependencyProperty.Register("MinRepetitions", typeof(int), typeof(LineTypeViewer), new FrameworkPropertyMetadata(3, OnPropertyChanged));

Also... since it pertains to the meta data described in the above properties, here are the callback functions I'm using:

private static void OnPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{   //Update the layout when any of the above properties are changed.
    LineTypeViewer viewer = (LineTypeViewer)source;
    viewer.UpdateLayout();
}

private static object OnCoerceDrawingWidthProperty(DependencyObject source, object data)
{
    if (data is float) //The DrawingWidth must be a float.
    {
        float dw = (float)data;
        if (dw < 0) return 5; //The DrawingWidth cannot be negative.
        else return dw;
    }
    else return 5;
}

And here are all of the wrapper properties:

public double Thickness
{
    get { return (double)GetValue(ThicknessProperty); }
    set { SetValue(ThicknessProperty, value); }
}
public SolidColorBrush Foreground
{
    get { return GetValue(ForegroundProperty) as SolidColorBrush; }
    set { SetValue(ForegroundProperty, value); }
}
public LineType CurrentLineType
{
    get { return GetValue(CurrentLineTypeProperty) as LineType; }
    set { SetValue(CurrentLineTypeProperty, value); }
}
public float DrawingWidth
{
    get { return (float)GetValue(DrawingWidthProperty); }
    set { SetValue(DrawingWidthProperty, value); }
}
public int MinRepetitions
{
    get { return (int)GetValue(MinRepetitionsProperty); }
    set { SetValue(MinRepetitionsProperty, value); }
}  

Based off of the guide's I've been using ( MDSN & WPF Tutorial ), my code appears correct, so perhaps it isn't a problem with dependency properties. My suspicion is that it is though because the dependency properties allow you customize your control via design time in Visual Studio. And it seems oddly coincidental that I cannot expand the Other Properties as mentioned before.

Your help with this is greatly appreciated! Thanks. :)

FrameworkPropertyMetadata cannot enforce the correct type for the default value of the property, this means that you need to make sure to provide the exact type expected. eg

DependencyProperty.Register
(
    "Thickness",
    typeof(double),
    typeof(LineTypeViewer),
    new FrameworkPropertyMetadata(1, OnPropertyChanged)
);

Here you provide an int to a double property. You need to change 1 to 1.0 or cast it (double)1 .

This may or may not be the reason for the crashes, but it should result in run-time errors nontheless.

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