RLH
RLH

Reputation: 15698

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

I have a XAML DatePicker that is defined as such:

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

I need to attach an event handler in my code behind to Binding.TargetUpdated and Binding.SourceUpdated events on the Binding object in my DatePicker.SelectedDate object.

However, when I re-define my tag as such:

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

I get the error messages:

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

and

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

What do I need to do to bind to these events? I understand the error message, but I don't know how it relates to the <DatePicker.SelectedDate> item since it should be a DependencyObject.

Upvotes: 0

Views: 2798

Answers (1)

dkozl
dkozl

Reputation: 33364

As mentioned in the comment you need to set event against DatePicker instead of Binding. Also, against the Binding, you need to enable both NotifyOnTargetUpdated and NotifyOnSourceUpdated in order for the events to be raised.

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

Upvotes: 1

Related Questions