2013-06-25 99 views
0

我正在使用WPF擴展工具包,並且我想知道是否可以隱藏屏蔽,然後在用戶輸入時,MaskedTextBox開始屏蔽文本。屏蔽文本框 - 隱藏屏蔽

默認設置是將掩碼顯示爲文本。例如,掩碼是

(99)999-9999 

默認的文本將是:

(__)___-____ 

我想空白文本,就像一個JavaScript的面具。

編輯:

我已經試圖改變ClipboardMaskFormat爲 「ExcludePromptAndLiterals」 和 「HidePromptOnLeave」 爲真,但不工作。

回答

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

0

我有類似的問題。我需要刪除所有的「_」字符,所以當他/她在maskedtextbox中鍵入IP時,客戶端不會感到困惑。我所做的是

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

我設置PromptChat爲「」(空格)和完美。