我在其中一個名爲「DateRangeSelector」的用戶控件中創建了一個DateTime依賴項屬性,並嘗試在另一個用戶控件(另一個xaml)中使用相同的屬性。但獲取Null引用異常。 下面是依賴屬性註冊的代碼,並強制值回調(我不得不使用裹脅值回調因爲爲propertyChange不工作)具有DateTime依賴項屬性的空引用異常
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;
}
這裏是在使用該控件的XAML:
<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}"
/>
但是我在xaml中得到異常:對象引用未設置爲對象的實例。 任何指針?
UPDATE:在綁定
「TodayDate」 在視圖模型被定義爲:
公衆的DateTime? TodayDate {get {return m_TodayDate; }設置
{ m_TodayDate = value; OnPropertyChanged(「TodayDate」); }}
綁定中的「TodayDate」在視圖模型中定義爲:public DateTime? TodayDate { get {return m_TodayDate; } set { m_TodayDate = value; OnPropertyChanged(「TodayDate」); } } – Sandeep
@Sandeep:你將它綁定到元素,而不是viewmodel。 –
@Sandeep:任何運氣?需要更多幫助? –