获取"类型必须来自DependencyObject"我尝试在DatePicker上设置TargetUpdated事件时出错

时间:2015-02-25 15:04:05

标签: wpf xaml events binding 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>

我需要将代码中的事件处理程序附加到Binding.TargetUpdated对象中Binding.SourceUpdated对象上的BindingDatePicker.SelectedDate事件。

然而,当我重新定义我的标签时:

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

我收到错误消息:

  

附加属性“TargetUpdated”只能应用于派生自“DependencyObject”的类型。

  

附加属性“SourceUpdated”只能应用于派生自“DependencyObject”的类型。

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

1 个答案:

答案 0 :(得分:1)

如评论中所述,您需要针对DatePicker而不是Binding设置事件。另外,针对Binding,您需要同时启用NotifyOnTargetUpdatedNotifyOnSourceUpdated才能引发事件。

<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>