none
DatePicker SelectedDateChanged event fire twice

    Question

  • My problem is simple. When i change the date from datepicker  datepicker's SelectedDateChanged fires once but when i change the date from code datepicker's SelectedDateChanged fires twice.i dont want to fire this event twice.

     

    my code which changed the date :

     

     

    private void btnDateLeft_Click(object sender, RoutedEventArgs e)
    
            {
    
                dteDate.SelectedDate = Convert.ToDateTime(dteDate.SelectedDate).AddDays(-1);
    
            }

    tag wpf c#

     

     

    Saturday, January 22, 2011 1:28 PM

Answers

  • Hi nzmhtpgl,

    When I change the date from code,I find the datepicker's SelectedDateChanged  twice calling are different in the stack through checking the stack .

    First calling datepicker's  SelectedDateChanged ,there is a  SelectedDateChanged event of  Calendar is called ,this event occurs when the collection returned by the SelectedDate property is changed ,then the datepicker's SelectedDateChanged fires once . The Second calling ,it has not Calendar’s SelectedDateChanged event,so the datepicker's SelectedDateChanged fires twice.

    If you want to call the datepicker's SelectedDateChanged only once,you can do this following.

            private void dteDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)

            {

                dteDate.SelectedDateChanged -= dteDate_SelectedDateChanged;

                MessageBox.Show("test");   

            }

     

            private void btnDateLeft_Click(object sender, RoutedEventArgs e)

            {

                dteDate.SelectedDateChanged += dteDate_SelectedDateChanged;

                dteDate.SelectedDate = Convert.ToDateTime(dteDate.SelectedDate).AddDays(-1);

            }

     

    • Marked as answer by nzmhtpgl Monday, January 24, 2011 3:02 PM
    Monday, January 24, 2011 8:30 AM
  • Hi nzmhtpgl,

    Because of this control’s character, you would better use a flag though you don’t like it.

    Add this code, hope you resolve it.

    public int flag = 0;

    private void dteDate_CalendarOpened(object sender, RoutedEventArgs e)

    {

         if (flag > 0)

         {

              datePicker1.SelectedDateChanged += datePicker1_SelectedDateChanged;

         }

         flag++;

    }

    • Marked as answer by nzmhtpgl Friday, January 28, 2011 8:15 PM
    Thursday, January 27, 2011 2:08 AM

All replies

  • Are you sure you did not twice added the same to handle event to the method.
    Success
    Cor
    Saturday, January 22, 2011 1:33 PM
  • Yes i am sure.If i twice added the same to handle event,it has fired twice when i changed date from datepicker too
    Saturday, January 22, 2011 1:34 PM
  • Sorry,

    Wrong readed, in those cases I simply remove the handler in the begin of the method and add it again add the end. 

    Takes to much time to investigate why (for instance what the AddDays does internally).

     


    Success
    Cor
    Saturday, January 22, 2011 1:50 PM
  • i removed the handler and added again.But the bug still on. i think it is a bug in wpf
    Saturday, January 22, 2011 1:55 PM
  • Hi nzmhtpgl,

    When I change the date from code,I find the datepicker's SelectedDateChanged  twice calling are different in the stack through checking the stack .

    First calling datepicker's  SelectedDateChanged ,there is a  SelectedDateChanged event of  Calendar is called ,this event occurs when the collection returned by the SelectedDate property is changed ,then the datepicker's SelectedDateChanged fires once . The Second calling ,it has not Calendar’s SelectedDateChanged event,so the datepicker's SelectedDateChanged fires twice.

    If you want to call the datepicker's SelectedDateChanged only once,you can do this following.

            private void dteDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)

            {

                dteDate.SelectedDateChanged -= dteDate_SelectedDateChanged;

                MessageBox.Show("test");   

            }

     

            private void btnDateLeft_Click(object sender, RoutedEventArgs e)

            {

                dteDate.SelectedDateChanged += dteDate_SelectedDateChanged;

                dteDate.SelectedDate = Convert.ToDateTime(dteDate.SelectedDate).AddDays(-1);

            }

     

    • Marked as answer by nzmhtpgl Monday, January 24, 2011 3:02 PM
    Monday, January 24, 2011 8:30 AM
  • Try this my example, which uses a flag. You set your own flag, which sets using or not using some of the methods or events. 

    You can find it here:http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/cc60f024-2e33-4c5b-9b6b-0c44061f1dbb (on the buttom).

    Monday, January 24, 2011 8:34 AM
  • Thank you for your interest.i can do it like your advise but i dont like using flag for situations like this. Milo Ren's advice is worked perfect
    Monday, January 24, 2011 3:19 PM
  • Hi Milo Ren,

    Your answer is right but adding dteDate.SelectedDateChanged -= dteDate_SelectedDateChanged; in the dteDate_SelectedDateChanged

    scope is problem when i changed date from datepicker not fire the selecteddatechange event

    Wednesday, January 26, 2011 11:29 AM
  • Hi nzmhtpgl,

    Because of this control’s character, you would better use a flag though you don’t like it.

    Add this code, hope you resolve it.

    public int flag = 0;

    private void dteDate_CalendarOpened(object sender, RoutedEventArgs e)

    {

         if (flag > 0)

         {

              datePicker1.SelectedDateChanged += datePicker1_SelectedDateChanged;

         }

         flag++;

    }

    • Marked as answer by nzmhtpgl Friday, January 28, 2011 8:15 PM
    Thursday, January 27, 2011 2:08 AM
  • Thank you
    Friday, January 28, 2011 8:15 PM
  • i founded the solution  

    1- delete selectedchanged event from datepicker .

    2- add mouseUp event .

    3-make if condition 

     if (e.Key == Key.Enter)
                {
                    if (dtp_StartDate.SelectedDate == null || dtp_EndDate.SelectedDate == null)
                    {
                    }
                    else
                    {
                        if (dtp_StartDate.SelectedDate <= dtp_EndDate.SelectedDate)
                        {
                            FillControll_cbx_OrderSerial();
                        }
                        else
                        {
                            DatePicker dt = (DatePicker)sender;
    
                            System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show(PerfectPos.Resources.Msg_Resources.SearchDateRange, PerfectPos.Resources.Msg_Resources.WarningMsg, System.Windows.Forms.MessageBoxButtons.YesNo);
                            if (dt.Name == "dtp_StartDate")
                            {
                                dtp_StartDate.Focus();
                            }
                            else if (dt.Name == "dtp_EndDate")
                            {
                                dtp_EndDate.Focus();
                            }
                        }
                    }
                }
    so the Fillcombo method will executed when user clicked Enter
    Sunday, September 08, 2013 1:12 PM