WPF DatePicker presented text internal bug

18 בMay 2017

אין תגובות

WPF Datepicker shows text before any date selected and that ‘Select a Date’

Now, if you working with custom template or other language or whatever,

You can do that while overwriting the Control template,

Than it founds interesting unit called PART_TextBox of type > DatePickerTextBox

Well, this control present the selected Date as a Text, and whatever you will set in this section of template will be the startup text instead of ‘Select a Date’ like ‘peek some date’

The problem is, lets say you set any date than you want to reset your Form, I mean to return this control to its original nature, now the control decided to manage your text and return the text to of ‘Select a Date’ instate of the text you have been placed in its template

(‘peek some date’). The problem is even deeper since you can try almost anything, like subscribe the Selection-Date-Change and again overwrite this text , but nothing.

The solution is an hookup a little. I am taking pointer to this DatePickerTextBox and than to its TextChenged event.

And check whatever you want , the default is null or empty (when setting the control selected date to null, it raise the bug .. and reset to default), And from other Thread call that control and manipulate its text again to what we need.

Something like:

private void FromDateTime_SelectedDateChanged(object sender,

       SelectionChangedEventArgs e)

       {

           DatePicker _datePicker1 = sender as DatePicker;

           DatePickerTextBox _datePickerTextBox = _datePicker1.Template.FindName("PART_TextBox", _datePicker1) as DatePickerTextBox;


           if (_datePickerTextBox != null)

           {

               _datePickerTextBox.TextChanged += (s, arg) =>

               {

                   if (_datePicker1.Text == "")

                   {

                       Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.DataBind, new Action(() => { _datePickerTextBox.Text = Application.Current.Resources["SelectDateString"] as string; }));

                   }

               };

           }

       }

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *