Quantcast
Channel: Silverlight 5 forum
Viewing all articles
Browse latest Browse all 1083

DatePicker Problem

$
0
0

I have meshed the DatePicker and TimePicker controls together to create a DateTimePicker. This control has served us fairly well over time but I'm finding a small glitch in the way the control behaves. My control (DateTimePicker) has a dependency property called "Value" which is a DateTime and encapsulates the date and time from the two child controls. The glitch is that for some reason the SelectedDateChanged event is firing on the DatePicker even though it shouldn't. At the point where the SelectedDate is set on the DatePicker, the event handler has been removed.

Here is the code for the a dependency property:

public static void ValueChangedEventCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    //The new value being set on the control
    var newValue = (DateTime)e.NewValue;

    //Get the datetimepicker
    var theDateTimePicker = (DateTimePicker)sender;

    //Stop events from firing while we set the values on the physical controls so that we don't get recursive loops
    theDateTimePicker.RemoveHandlers();

    //Set the values on the physical controls
    if (newValue == DateTime.MinValue || (newValue.Year == 1753 && newValue.Month == 1 && newValue.Day == 1))
    {
        theDateTimePicker.TheDate.SelectedDate = null; ;
        theDateTimePicker.TheTime.Value = null; ;
    }
    else
    {
        theDateTimePicker.TheDate.SelectedDate = newValue;
        theDateTimePicker.TheTime.Value = newValue;
    }

    //Put the event handlers back on the controls
    theDateTimePicker.AddHandlers();
}

Here is the code for RemoveHandlers/AddHandlers (these methods are an attempt to turn the event firing on/off)

internal void AddHandlers()
{
    TheDate.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(TheDate_SelectedDateChanged);
    TheTime.ValueChanged += new RoutedPropertyChangedEventHandler<DateTime?>(TheTime_ValueChanged);
}

internal void RemoveHandlers()
{
    TheDate.SelectedDateChanged -= new EventHandler<SelectionChangedEventArgs>(TheDate_SelectedDateChanged);
    TheTime.ValueChanged -= new RoutedPropertyChangedEventHandler<DateTime?>(TheTime_ValueChanged);
}
Theoretically, the event handler TheDate_SelectedDateChanged should only get called if the user physically selects a date on the control. However, this event handler is actually firing after the ValueChangedEvenntCallback. I don't know why...

How can I avoid this?


Viewing all articles
Browse latest Browse all 1083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>