I have created a DateTime dependency property in one of the usercontrol named "DateRangeSelector" and trying to use the same in another usercontrol(another xaml).But getting the Null Reference exception. Here is the code for the dependency property registration and coerce value callback(i had to use the coerce value callback because propertychange does not work)
public static readonly DependencyProperty TodayDateProperty = DependencyProperty.Register("TodayDate", typeof(DateTime?), typeof(DateRangeSelectorControl),
new PropertyMetadata(null, null, TodayDateChanged));
private static object TodayDateChanged(DependencyObject d, object baseValue)
{
((DateRangeSelectorControl)d).TodayDateChanged((DateTime)baseValue);
return baseValue;
}
Here is the XAML where this control is used:
<DateRangeSelector:DateRangeSelectorControl
x:Name="DateRangeSelector"
Grid.Column="1"
Margin="10 0 0 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
AutomationProperties.AutomationId="AID_TaskListDateRangeSelector"
DateRangeUpdatedCmd="{Binding Path=DateRangeSelectionUpdatedCommand}"
FontSize="{StaticResource TaskListMenuFontSize}"
RangeOptions="{Binding Path=DateRangeSelectionOptions,
Mode=OneTime}"
SelectedDateRange="{Binding Path=SelectedRange,
Mode=TwoWay}"
Visibility="{Binding Path=ShowFilterOptions,
Converter={StaticResource boolToVisibility}}"
TodayDate="{Binding TodayDate, ElementName=DateRangeSelector}"
/>
But am getting exception in xaml as: Object reference not set to an instance of an object. Any pointers?
UPDATE:
"TodayDate" in the binding is defined in viewmodel as:
public DateTime? TodayDate { get { return m_TodayDate; } set
{ m_TodayDate = value; OnPropertyChanged("TodayDate"); } }