Disguise for choosing a date in WPF

I want to add masking to a WPF date control. I saw that DatePickerTextBox could not be expanded further.

So, I decided to add interactive behavior to it. For this, I used the following code:

Masked date picker class:

public class MaskedDatePicker : DatePicker { } 

and I created an attached behavior as shown below:

 public class DatePickerTextBoxInputMaskBehavior : Behavior<DatePickerTextBox> { } 

Now in the templates I have bound the behavior:

 <DatePickerTextBox x:Name="PART_TextBox" Grid.Row="0" Grid.Column="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Focusable="{TemplateBinding Focusable}" Foreground="{TemplateBinding Foreground}"> <i:Interaction.Behaviors> <cleanPoc:DatePickerTextBoxInputMaskBehavior /> </i:Interaction.Behaviors> </DatePickerTextBox> 

Now every time I open a calendar from a datepicker application, it freezes because the text changed in DatePickerTextBoxInputMaskBehavior starts recursively. Any idea how to handle this?

+5
source share
1 answer

The base control (DatePicker) performs operations on a DatePickerTextBox that you cannot control by adding behavior to a DatePickerTextBox ...

Get a reference to the MaskedDatePicker element itself and set the Text property to DatePickerTextBox when the control's SelectedDate property gets a new value. Take a look at the following example and let me know if you need clarification.

Good luck

  public class DatePickerTextBoxInputMaskBehavior : Behavior<DatePickerTextBox> { ... containing the event data. private void AssociatedObjectLoaded(object sender, System.Windows.RoutedEventArgs e) { this.Provider = new MaskedTextProvider(this.InputMask, CultureInfo.CurrentCulture); this.Provider.Set(this.AssociatedObject.Text); this.Provider.PromptChar = this.PromptChar; this.SetText(this.Provider.ToDisplayString()); MaskedDatePicker dp = FindVisualParent<MaskedDatePicker>(this.AssociatedObject); var textProp = DependencyPropertyDescriptor.FromProperty(MaskedDatePicker.SelectedDateProperty, typeof(MaskedDatePicker)); if (textProp != null) { textProp.AddValueChanged(dp, OnHandler); } } private static T FindVisualParent<T>(DependencyObject dependencyObject) where T : DependencyObject { var parent = VisualTreeHelper.GetParent(dependencyObject); if (parent == null) return null; var parentT = parent as T; return parentT ?? FindVisualParent<T>(parent); } private void OnHandler(object s, EventArgs args) { this.UpdateText(); } private void UpdateText() { if (this.Provider.ToDisplayString().Equals(this.AssociatedObject.Text)) { return; } MaskedDatePicker dp = FindVisualParent<MaskedDatePicker>(this.AssociatedObject); if (dp != null && dp.SelectedDate.HasValue) SetText(dp.SelectedDate.Value.ToString("dd/MM/yyyy")); //format date here... } /// <summary> /// Sets the text. /// </summary> /// <param name="text">The text.</param> private void SetText(string text) { this.AssociatedObject.Text = string.IsNullOrWhiteSpace(text) ? string.Empty : string.Format(CultureInfo.CurrentCulture, text.ToString(CultureInfo.CurrentCulture.DateTimeFormat)); } } } 
+1
source

Source: https://habr.com/ru/post/1201067/


All Articles