I created a UserControl with a DependencyProperty. I set a value for this UserControl on FormLoad. But when I run the application no value is shown.
My code:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(PersianDateTime), typeof(UCDatePicker),
new PropertyMetadata(PersianDateTime.Now, new PropertyChangedCallback((onchangedcallback))));
public PersianDateTime Value
{
get { return (PersianDateTime)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static void onchangedcallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
UCDatePicker control = obj as UCDatePicker;
PersianDateTime newdate = (PersianDateTime)e.NewValue;
control.Years = ((PersianDateTime)e.NewValue).Year;
control.Months = ((PersianDateTime)e.NewValue).Month;
control.Days = ((PersianDateTime)e.NewValue).Day;
control.Hours = ((PersianDateTime)e.NewValue).Hour;
control.Minutes = ((PersianDateTime)e.NewValue).Minute;
}
In my FormLoad:
uCDatePicker1.Value = PersianDateTime.Now;
when i use breakpoint ,onchangedcallback doesn’t execute.
Because you don’t change value,
Value is equal to
PersianDateTime.Nowbefore and after your assignmentonchangedcallback will be called only if you will assign different value to dependency property, you can change default value of dependency property to some default value if you want property changed event to be called at first assignment
If PersianDateTime is reference type(class for example) change your dependency property like this
But looking on PersianDateTime type, seems it is struct so you can use
and it will work
Hope this helps