background
Hi , today I am going to share a small tips, its about dependency property, here is what I added in my presenter, looks like a innocent dependency property to me.
CenterX DependencyProperty
public double CenterX
{
get { return (double)GetValue(CenterXProperty); }
set { SetValue(CenterXProperty, value); }
}
public static readonly DependencyProperty CenterXProperty =
DependencyProperty.Register("CenterX", typeof(double),
typeof(MainWindowPresenter), new UIPropertyMetadata(0));
In first thought I found not problem in this code, but when I run the code end up with a the following exception,
In fact I didn’t now that it happen because of the dependency property, I was banging my head on the wall for few minutes when I saw this exception window. I commended lots of code that I wrote since the last commit and successful run. And when I start commenting one by one code segment, and finally found out that dependency property.
The want’s wrong with it?
It took a while to figure out what is wrong with it, I have rewritten the property with a minor change, can you see what is changed in the following code?
public double CenterX
{
get { return (double)GetValue(CenterXProperty); }
set { SetValue(CenterXProperty, value); }
}
public static readonly DependencyProperty CenterXProperty =
DependencyProperty.Register("CenterX", typeof(double),
typeof(MainWindowPresenter), new UIPropertyMetadata(0d));
Yes a simple change first time it was 0 how 0d on UIPropertyMetaData. Yes this is a simple change but took a while to figure it out.
Hope you wont loss you time with this kind of mistake, always use 0d or some (doublevalue)d when initializing a double dependency property.
Until next time bye bye.