当我尝试在 DatePicker 上设置 TargetUpdated 事件时出现 "Type must derive from DependencyObject" 错误

Getting an "Type must derive from DependencyObject" error, when I try to to set a TargetUpdated event on a DatePicker

我有一个 XAML DatePicker 是这样定义的:

<DatePicker x:Name="startDateDatePicker" Validation.ErrorTemplate="{StaticResource validationTemplate}"
SelectedDateChanged="window_DatePicker_SelectedDateChanged" Validation.Error="ValidationError">
    <DatePicker.SelectedDate>
        <Binding Path="startDate" Mode="TwoWay" NotifyOnValidationError="True" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <!-- A couple custom rules. //-->
            </Binding.ValidationRules>
         </Binding>
    </DatePicker.SelectedDate>
</DatePicker>

我需要在我的 DatePicker.SelectedDate 对象中的 Binding 对象的 Binding.TargetUpdatedBinding.SourceUpdated 事件中附加一个事件处理程序。

但是,当我这样重新定义我的标签时:

<Binding Path="startDate" Mode="TwoWay" NotifyOnValidationError="True" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged" TargetUpdated="BindingTargetUpdated" SourceUpdated="BindingSourceUpdaed">

我收到错误消息:

The attached property "TargetUpdated" can only be applied to types that are derived from "DependencyObject".

The attached property "SourceUpdated" can only be applied to types that are derived from "DependencyObject".

我需要做什么才能绑定到这些事件?我理解错误消息,但我不知道它与 <DatePicker.SelectedDate> 项目有什么关系,因为它应该是 DependencyObject.

如评论中所述,您需要针对 DatePicker 而不是 Binding 设置事件。此外,针对 Binding,您需要启用两个 NotifyOnTargetUpdated and NotifyOnSourceUpdated 才能引发事件。

<DatePicker ... TargetUpdated="BindingTargetUpdated" SourceUpdated="BindingSourceUpdaed">
    <DatePicker.SelectedDate>
        <Binding Path="startDate" ... NotifyOnTargetUpdated="True" NotifyOnSourceUpdated="True">
            <Binding.ValidationRules>
                <!-- A couple custom rules. //-->
            </Binding.ValidationRules>
         </Binding>
    </DatePicker.SelectedDate>
</DatePicker>