0

我正在使用 WPF 扩展工具包,我想知道是否可以隐藏掩码,然后在用户键入时,MaskedTextBox 开始屏蔽文本。

默认设置是将掩码显示为文本。例如,掩码是

 (99)999-9999

默认文本为:

 (__)___-____

我想要空白文本,例如 javascript 掩码。

编辑:

我已经尝试将 ClipboardMaskFormat 更改为“ExcludePromptAndLiterals”并将“HidePromptOnLeave”更改为 true,但不起作用。

4

2 回答 2

2

我猜你可以用Behavior<MaskedTextBox>

就像是:

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;
  }
}

和用法:

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

现在MaskedTextBox提示格式只有在它有焦点或它有任何有效时才可见Value

于 2013-06-25T19:21:23.990 回答
0

我有一个类似的问题。我需要删除所有“_”字符,这样客户在他/她在 maskedtextbox 中键入 IP 时就不会感到困惑。我所做的是

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

我将 PromptChat 设置为“”(一个空格)并且工作得很好。

于 2015-07-28T19:55:36.837 回答