Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The default value type does not match the type of the property

I have this class

public class Tooth {     public string Id {get;set;} } 

And this custrom control

public partial class ToothUI : UserControl {     public ToothUI()     {         InitializeComponent();     }      public Tooth Tooth     {         get { return (Tooth)GetValue(ToothProperty); }         set         {             SetValue(ToothProperty, value);             NombrePieza.Text =   value.Id.Replace("_",String.Empty);         }     }     public static readonly DependencyProperty ToothProperty =         DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI), new PropertyMetadata(0));   } 

My problem is after Add Tooth dependency property, this error happen

The default value type does not match the type of the property

What exactly this error mean? What is the current way to set this DP

like image 806
Juan Pablo Gomez Avatar asked Dec 05 '13 11:12

Juan Pablo Gomez


People also ask

What is the default value of the property?

Definition and Usage Note: The default value is the value specified in the HTML value attribute. The difference between the defaultValue and value property, is that defaultValue contains the default value, while value contains the current value after some changes have been made.

How do I set default value in property?

Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.

Can we set default value in property C#?

You can assign the default value using the DefaultValueAttribute attribute, as shown below.


2 Answers

Default value for DP does not match your type.

Change

public static readonly DependencyProperty ToothProperty =         DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI),                                          new PropertyMetadata(0)); 

to

public static readonly DependencyProperty ToothProperty =         DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI),                                       new PropertyMetadata(default(Tooth))); 

Or simply omit setting default value for your DP:

public static readonly DependencyProperty ToothProperty =         DependencyProperty.Register("Tooth", typeof(Tooth), typeof(ToothUI)); 
like image 54
Rohit Vats Avatar answered Oct 05 '22 23:10

Rohit Vats


I came here for the title of the question but my type was a decimal default value and i solved with this 0.0M https://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

like image 25
dpineda Avatar answered Oct 05 '22 23:10

dpineda