Guilherme
Guilherme

Reputation: 5341

Masked Text Box - Hide Mask

I'm using the WPF Extended Toolkit, and I want to know if it's possible to hide the mask, and then while the user is typing, the MaskedTextBox starts to mask the text.

The default settings is to show the mask as text. For example, mask is

 (99)999-9999

the default text will be:

 (__)___-____

I want blank text, like a javascript mask.

Edit:

I already tried changing ClipboardMaskFormat to "ExcludePromptAndLiterals" and "HidePromptOnLeave" to true, but doesn't work.

Upvotes: 0

Views: 3932

Answers (2)

mito
mito

Reputation: 61

I had a similar problem. I needed to delete all the "_" characters, so the client won't be confused at the time that he/she types into the maskedtextbox an IP. What I did was

<wpx:MaskedTextBox IncludePromptInValue="True" IncludeLiteralsInValue="False" Mask="000,000,000,000" PromptChar=" "/>

I set PromptChat as " " (a blank space) and worked perfectly.

Upvotes: 0

Viv
Viv

Reputation: 17380

I'd guess you can do this with a Behavior<MaskedTextBox>

something like:

public class MaskVisibilityBehavior : Behavior<MaskedTextBox> {
  private FrameworkElement _contentPresenter;

  protected override void OnAttached() {
    base.OnAttached();
    AssociatedObject.Loaded += (sender, args) => {
      _contentPresenter = AssociatedObject.Template.FindName("PART_ContentHost", AssociatedObject) as FrameworkElement;
      if (_contentPresenter == null)
        throw new InvalidCastException();
      AssociatedObject.TextChanged += OnTextChanged;
      AssociatedObject.GotFocus += OnGotFocus;
      AssociatedObject.LostFocus += OnLostFocus;
      UpdateMaskVisibility();
    };
  }

  protected override void OnDetaching() {
    AssociatedObject.TextChanged -= OnTextChanged;
    AssociatedObject.GotFocus -= OnGotFocus;
    AssociatedObject.LostFocus -= OnLostFocus;
    base.OnDetaching();
  }

  private void OnLostFocus(object sender, RoutedEventArgs routedEventArgs) {
    UpdateMaskVisibility();
  }

  private void OnGotFocus(object sender, RoutedEventArgs routedEventArgs) {
    UpdateMaskVisibility();
  }

  private void OnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs) {
    UpdateMaskVisibility();
  }

  private void UpdateMaskVisibility() {
    _contentPresenter.Visibility = AssociatedObject.MaskedTextProvider.AssignedEditPositionCount > 0 ||
                                    AssociatedObject.IsFocused
                                      ? Visibility.Visible
                                      : Visibility.Hidden;
  }
}

and usage:

<xctk:MaskedTextBox Mask="(000) 000-0000"
                    ValueDataType="{x:Type s:String}">
  <i:Interaction.Behaviors>
    <local:MaskVisibilityBehavior />
  </i:Interaction.Behaviors>
</xctk:MaskedTextBox>

Now the MaskedTextBox hint format will only be visible either when it has focus or it has any valid Value in it.

Upvotes: 2

Related Questions