2009-07-09 13 views
52

मैं टेक्स्टबॉक्स को केवल कैपिटल अक्षरों को स्वीकार करने के लिए प्रतिबंधित कर सकता हूं, या उदाहरण के लिए, या किसी विशेष चरित्र को रखने से मना कर सकता हूं?टेक्स्टबॉक्स इनपुट प्रतिबंधों को कैसे परिभाषित करें?

निश्चित रूप से यह टेक्स्ट इनपुट ईवेंट को पकड़ने और यहां पाठ को संभालने के लिए केक का एक टुकड़ा है, लेकिन क्या यह ऐसा करने का उचित तरीका है?

उत्तर

110

मैं एक संलग्न व्यवहार है, जो इस तरह इस्तेमाल किया जा सकता के साथ अतीत में इस किया है:

<TextBox b:Masking.Mask="^\p{Lu}*$"/> 

संलग्न व्यवहार कोड इस तरह दिखता है:

/// <summary> 
/// Provides masking behavior for any <see cref="TextBox"/>. 
/// </summary> 
public static class Masking 
{ 
    private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression", 
     typeof(Regex), 
     typeof(Masking), 
     new FrameworkPropertyMetadata()); 

    /// <summary> 
    /// Identifies the <see cref="Mask"/> dependency property. 
    /// </summary> 
    public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask", 
     typeof(string), 
     typeof(Masking), 
     new FrameworkPropertyMetadata(OnMaskChanged)); 

    /// <summary> 
    /// Identifies the <see cref="MaskExpression"/> dependency property. 
    /// </summary> 
    public static readonly DependencyProperty MaskExpressionProperty = _maskExpressionPropertyKey.DependencyProperty; 

    /// <summary> 
    /// Gets the mask for a given <see cref="TextBox"/>. 
    /// </summary> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask is to be retrieved. 
    /// </param> 
    /// <returns> 
    /// The mask, or <see langword="null"/> if no mask has been set. 
    /// </returns> 
    public static string GetMask(TextBox textBox) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     return textBox.GetValue(MaskProperty) as string; 
    } 

    /// <summary> 
    /// Sets the mask for a given <see cref="TextBox"/>. 
    /// </summary> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask is to be set. 
    /// </param> 
    /// <param name="mask"> 
    /// The mask to set, or <see langword="null"/> to remove any existing mask from <paramref name="textBox"/>. 
    /// </param> 
    public static void SetMask(TextBox textBox, string mask) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     textBox.SetValue(MaskProperty, mask); 
    } 

    /// <summary> 
    /// Gets the mask expression for the <see cref="TextBox"/>. 
    /// </summary> 
    /// <remarks> 
    /// This method can be used to retrieve the actual <see cref="Regex"/> instance created as a result of setting the mask on a <see cref="TextBox"/>. 
    /// </remarks> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask expression is to be retrieved. 
    /// </param> 
    /// <returns> 
    /// The mask expression as an instance of <see cref="Regex"/>, or <see langword="null"/> if no mask has been applied to <paramref name="textBox"/>. 
    /// </returns> 
    public static Regex GetMaskExpression(TextBox textBox) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     return textBox.GetValue(MaskExpressionProperty) as Regex; 
    } 

    private static void SetMaskExpression(TextBox textBox, Regex regex) 
    { 
     textBox.SetValue(_maskExpressionPropertyKey, regex); 
    } 

    private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     var textBox = dependencyObject as TextBox; 
     var mask = e.NewValue as string; 
     textBox.PreviewTextInput -= textBox_PreviewTextInput; 
     textBox.PreviewKeyDown -= textBox_PreviewKeyDown; 
     DataObject.RemovePastingHandler(textBox, Pasting); 

     if (mask == null) 
     { 
      textBox.ClearValue(MaskProperty); 
      textBox.ClearValue(MaskExpressionProperty); 
     } 
     else 
     { 
      textBox.SetValue(MaskProperty, mask); 
      SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace)); 
      textBox.PreviewTextInput += textBox_PreviewTextInput; 
      textBox.PreviewKeyDown += textBox_PreviewKeyDown; 
      DataObject.AddPastingHandler(textBox, Pasting); 
     } 
    } 

    private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     var proposedText = GetProposedText(textBox, e.Text); 

     if (!maskExpression.IsMatch(proposedText)) 
     { 
      e.Handled = true; 
     } 
    } 

    private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     //pressing space doesn't raise PreviewTextInput - no idea why, but we need to handle 
     //explicitly here 
     if (e.Key == Key.Space) 
     { 
      var proposedText = GetProposedText(textBox, " "); 

      if (!maskExpression.IsMatch(proposedText)) 
      { 
       e.Handled = true; 
      } 
     } 
    } 

    private static void Pasting(object sender, DataObjectPastingEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     if (e.DataObject.GetDataPresent(typeof(string))) 
     { 
      var pastedText = e.DataObject.GetData(typeof(string)) as string; 
      var proposedText = GetProposedText(textBox, pastedText); 

      if (!maskExpression.IsMatch(proposedText)) 
      { 
       e.CancelCommand(); 
      } 
     } 
     else 
     { 
      e.CancelCommand(); 
     } 
    } 

    private static string GetProposedText(TextBox textBox, string newText) 
    { 
     var text = textBox.Text; 

     if (textBox.SelectionStart != -1) 
     { 
      text = text.Remove(textBox.SelectionStart, textBox.SelectionLength); 
     } 

     text = text.Insert(textBox.CaretIndex, newText); 

     return text; 
    } 
} 
+27

वाह .. गंदगी .. कारण है कि यह इतना मुश्किल है? – Agzam

+0

मैं यह नहीं कहूंगा कि यह था। मैंने इसे आपकी तुलना में अधिक लचीला होने के लिए लिखा था। अवधारणा को लेने और अपनी आवश्यकताओं के लिए सरल बनाने के लिए स्वतंत्र महसूस करें। –

+1

बहुत अच्छा, केंट। टेक्स्टबॉक्स में प्रवेश सीमित करने के लिए एक लचीला और शक्तिशाली समाधान। +1 –

2
private void TextBox1_SelectionChanged(object sender, RoutedEventArgs e) 
{ 
    string txt = TextBox1.Text; 
    if (txt != "") 
    { 
     TextBox1.Text = Regex.Replace(TextBox1.Text, "[^0-9]", ""); 
     if (txt != TextBox1.Text) 
     { 
      TextBox1.Select(TextBox1.Text.Length, 0); 
     } 
    } 
} 
+0

मुझे नहीं पता कि इसे एक कस्टम संपत्ति कैसे बनाएं, लेकिन यह एक आसान समाधान है। मैं सराहना करता हूं अगर कोई इसे केंट की तरह संपत्ति में परिवर्तित कर देगा। – WhoIsNinja

43

मैं निम्नलिखित कार्यों को संभालने के द्वारा केंट बूगार्ट के जवाब में भी सुधार किया है जो पैटर्न को उल्लंघन करने का कारण बन सकता है:

  • बैकस्पेस
  • चुनना और एक तरह से खींच पाठ उस पैटर्न का उल्लंघन कर सकता
  • कट आदेश

उदाहरण के लिए, केंट Boogaart का जवाब पहले "abc" दर्ज करके "ac" दर्ज करने के लिए उपयोगकर्ता की अनुमति दी

: और बाद में बैकस्पेस जो निम्नलिखित regex

^(a|ab|abc)$ 

प्रयोग (अपरिवर्तित) का उल्लंघन करता है के साथ "बी" को हटाना

<TextBox b:Masking.Mask="^\p{Lu}*$"/>

मास्क वर्ग:

public static class Masking 
{ 
    private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression", 
      typeof(Regex), 
      typeof(Masking), 
      new FrameworkPropertyMetadata()); 

    /// <summary> 
    /// Identifies the <see cref="Mask"/> dependency property. 
    /// </summary> 
    public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask", 
      typeof(string), 
      typeof(Masking), 
      new FrameworkPropertyMetadata(OnMaskChanged)); 

    /// <summary> 
    /// Identifies the <see cref="MaskExpression"/> dependency property. 
    /// </summary> 
    public static readonly DependencyProperty MaskExpressionProperty = _maskExpressionPropertyKey.DependencyProperty; 

    /// <summary> 
    /// Gets the mask for a given <see cref="TextBox"/>. 
    /// </summary> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask is to be retrieved. 
    /// </param> 
    /// <returns> 
    /// The mask, or <see langword="null"/> if no mask has been set. 
    /// </returns> 
    public static string GetMask(TextBox textBox) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     return textBox.GetValue(MaskProperty) as string; 
    } 

    /// <summary> 
    /// Sets the mask for a given <see cref="TextBox"/>. 
    /// </summary> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask is to be set. 
    /// </param> 
    /// <param name="mask"> 
    /// The mask to set, or <see langword="null"/> to remove any existing mask from <paramref name="textBox"/>. 
    /// </param> 
    public static void SetMask(TextBox textBox, string mask) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     textBox.SetValue(MaskProperty, mask); 
    } 

    /// <summary> 
    /// Gets the mask expression for the <see cref="TextBox"/>. 
    /// </summary> 
    /// <remarks> 
    /// This method can be used to retrieve the actual <see cref="Regex"/> instance created as a result of setting the mask on a <see cref="TextBox"/>. 
    /// </remarks> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask expression is to be retrieved. 
    /// </param> 
    /// <returns> 
    /// The mask expression as an instance of <see cref="Regex"/>, or <see langword="null"/> if no mask has been applied to <paramref name="textBox"/>. 
    /// </returns> 
    public static Regex GetMaskExpression(TextBox textBox) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     return textBox.GetValue(MaskExpressionProperty) as Regex; 
    } 

    private static void SetMaskExpression(TextBox textBox, Regex regex) 
    { 
     textBox.SetValue(_maskExpressionPropertyKey, regex); 
    } 

    private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     var textBox = dependencyObject as TextBox; 
     var mask = e.NewValue as string; 
     textBox.PreviewTextInput -= textBox_PreviewTextInput; 
     textBox.PreviewKeyDown -= textBox_PreviewKeyDown; 
     DataObject.RemovePastingHandler(textBox, Pasting); 
     DataObject.RemoveCopyingHandler(textBox, NoDragCopy); 
     CommandManager.RemovePreviewExecutedHandler(textBox, NoCutting); 


     if (mask == null) 
     { 
      textBox.ClearValue(MaskProperty); 
      textBox.ClearValue(MaskExpressionProperty); 
     } 
     else 
     { 
      textBox.SetValue(MaskProperty, mask); 
      SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace)); 
      textBox.PreviewTextInput += textBox_PreviewTextInput; 
      textBox.PreviewKeyDown += textBox_PreviewKeyDown; 
      DataObject.AddPastingHandler(textBox, Pasting); 
      DataObject.AddCopyingHandler(textBox, NoDragCopy); 
      CommandManager.AddPreviewExecutedHandler(textBox, NoCutting); 
     } 
    } 

    private static void NoCutting(object sender, ExecutedRoutedEventArgs e) 
    { 
     if(e.Command == ApplicationCommands.Cut) 
     { 
      e.Handled = true; 
     } 
    } 

    private static void NoDragCopy(object sender, DataObjectCopyingEventArgs e) 
    { 
     if (e.IsDragDrop) 
     { 
      e.CancelCommand(); 
     } 
    } 

    private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     var proposedText = GetProposedText(textBox, e.Text); 

     if (!maskExpression.IsMatch(proposedText)) 
     { 
      e.Handled = true; 
     } 
    } 

    private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     string proposedText = null; 

     //pressing space doesn't raise PreviewTextInput, reasons here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/446ec083-04c8-43f2-89dc-1e2521a31f6b?prof=required 
     if (e.Key == Key.Space) 
     { 
      proposedText = GetProposedText(textBox, " "); 
     } 
     // Same story with backspace 
     else if(e.Key == Key.Back) 
     { 
      proposedText = GetProposedTextBackspace(textBox); 
     } 

     if (proposedText != null && proposedText != string.Empty && !maskExpression.IsMatch(proposedText)) 
     { 
      e.Handled = true; 
     } 

    } 

    private static void Pasting(object sender, DataObjectPastingEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     if (e.DataObject.GetDataPresent(typeof(string))) 
     { 
      var pastedText = e.DataObject.GetData(typeof(string)) as string; 
      var proposedText = GetProposedText(textBox, pastedText); 

      if (!maskExpression.IsMatch(proposedText)) 
      { 
       e.CancelCommand(); 
      } 
     } 
     else 
     { 
      e.CancelCommand(); 
     } 
    } 

    private static string GetProposedTextBackspace(TextBox textBox) 
    { 
     var text = GetTextWithSelectionRemoved(textBox); 
     if (textBox.SelectionStart > 0 && textBox.SelectionLength == 0) 
     { 
      text = text.Remove(textBox.SelectionStart-1, 1); 
     } 

     return text; 
    } 


    private static string GetProposedText(TextBox textBox, string newText) 
    { 
     var text = GetTextWithSelectionRemoved(textBox); 
     text = text.Insert(textBox.CaretIndex, newText); 

     return text; 
    } 

    private static string GetTextWithSelectionRemoved(TextBox textBox) 
    { 
     var text = textBox.Text; 

     if (textBox.SelectionStart != -1) 
     { 
      text = text.Remove(textBox.SelectionStart, textBox.SelectionLength); 
     } 
     return text; 
    } 
} 
+0

आपने समाधान को 3 तरीकों से बेहतर किया है लेकिन इसके संक्षिप्त विवरण को पढ़ने से घंटी बजती नहीं है। मुझे लगता है कि इसे टेक्स्टबॉक्स में कॉपी पेस्टिंग टेक्स्ट के साथ करना है, लेकिन केंट के समाधान ने पहले ही दोषपूर्ण टेक्स्ट को अवरुद्ध कर दिया है। क्या आप शायद समझा सकते हैं कि सुधारों के मामलों में क्या सुधार होता है? धन्यवाद – buckley

+0

ये सुधार उपयोगकर्ता को रेगेक्स नियम का उल्लंघन करने से रोकते हैं। तो उदाहरण के लिए, इन परिवर्तनों से पहले, आप नियम को अवैध बनाने के लिए बैकस्पेस का उपयोग कर सकते हैं। तो यदि पैटर्न '^ 123 $' था तो आप '123' टाइप कर सकते हैं और फिर' 2' हटा सकते हैं और यह आपको देगा। अब यह नहीं है। अन्य सुधारों के लिए भी जाता है - पाठ काटना और चारों ओर पाठ खींचना। – VitalyB

+0

मैं देख सकता हूं कि अब आपका क्या मतलब है, लेकिन केवल एक कॉपी पेस्ट एक्शन उपयोगकर्ता को उस बिंदु पर प्राप्त कर सकता है जहां टेक्स्टबॉक्स में "123" शुरू होता है। केवल "1" (या कोई अन्य चरित्र) चरित्र टाइप करना असंभव होगा क्योंकि यह "^ 123 $" मान्य नहीं है। * टाइप किए गए * स्ट्रिंग में बैकस्पेस का उपयोग हमेशा पैटर्न का सम्मान करेगा क्योंकि पिछले इनपुट पहले ही मान्य हो चुका था। कुछ उपयोगकर्ता निश्चित रूप से पेस्ट की प्रतिलिपि बनाएंगे और आपका सुधार इस एज केस को संबोधित करेगा जो कुछ समय के लिए एक रहस्य होता। धन्यवाद – buckley

12

मैं रंग विषय-वस्तु का समर्थन करने के VitalyB के कोड बदल दिया है। उपयोगकर्ता इनपुट को अवरुद्ध करने के बजाय यदि यह RegEx स्क्रिप्ट को पूरा नहीं करता है, तो यह केवल टेक्स्ट बॉक्स को हाइलाइट करता है। टेक्स्ट बॉक्स इंटरैक्शन के बिना विषय डिफ़ॉल्ट होगा, और उसके बाद इनपुट सेट होने के बाद मान के आधार पर हल्के हरे या लाल पर डिफ़ॉल्ट होगा। तुम भी सेट कर सकते हैं असफल और साथ प्रोग्राम के रंग पारित:

b:ColorMasking.PassColor = "Hexadecimal Value" 
b:ColorMasking.FailColor = "Hexadecimal Value" 

वर्ग के नीचे है: स्क्रिप्ट एक वर्ग हारून सी द्वारा लिखित आवश्यकता

public class ColorMasking : DependencyObject 
{ 
    private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression", 
      typeof(Regex), 
      typeof(ColorMasking), 
      new FrameworkPropertyMetadata()); 

    /// <summary> 
    /// Identifies the <see cref="Mask"/> dependency property. 
    /// </summary> 
    /// 
    public static readonly DependencyProperty PassColorProperty = DependencyProperty.RegisterAttached("PassColor", 
      typeof(string), 
      typeof(ColorMasking), 
      new PropertyMetadata("#99FF99")); 

    public static void SetPassColor(DependencyObject obj, string passColor) 
    { 
     obj.SetValue(PassColorProperty, passColor); 
    } 

    public static string GetPassColor(DependencyObject obj) 
    { 
     return (string)obj.GetValue(PassColorProperty); 
    } 


    public static readonly DependencyProperty FailColorProperty = DependencyProperty.RegisterAttached("FailColor", 
      typeof(string), 
      typeof(ColorMasking), 
      new PropertyMetadata("#FFCCFF")); 

    public static void SetFailColor(DependencyObject obj, string failColor) 
    { 
     obj.SetValue(FailColorProperty, failColor); 
    } 

    public static string GetFailColor(DependencyObject obj) 
    { 
     return (string)obj.GetValue(FailColorProperty); 
    } 

    public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask", 
      typeof(string), 
      typeof(ColorMasking), 
      new FrameworkPropertyMetadata(OnMaskChanged)); 

    private static void OnPassColorChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     var textBox = dependencyObject as TextBox; 
     var color = e.NewValue as string; 
     textBox.SetValue(PassColorProperty, color); 
    } 

    /// <summary> 
    /// Identifies the <see cref="MaskExpression"/> dependency property. 
    /// </summary> 
    public static readonly DependencyProperty MaskExpressionProperty = _maskExpressionPropertyKey.DependencyProperty; 

    /// <summary> 
    /// Gets the mask for a given <see cref="TextBox"/>. 
    /// </summary> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask is to be retrieved. 
    /// </param> 
    /// <returns> 
    /// The mask, or <see langword="null"/> if no mask has been set. 
    /// </returns> 
    public static string GetMask(TextBox textBox) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     return textBox.GetValue(MaskProperty) as string; 
    } 

    /// <summary> 
    /// Sets the mask for a given <see cref="TextBox"/>. 
    /// </summary> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask is to be set. 
    /// </param> 
    /// <param name="mask"> 
    /// The mask to set, or <see langword="null"/> to remove any existing mask from <paramref name="textBox"/>. 
    /// </param> 
    public static void SetMask(TextBox textBox, string mask) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     textBox.SetValue(MaskProperty, mask); 
    } 

    /// <summary> 
    /// Gets the mask expression for the <see cref="TextBox"/>. 
    /// </summary> 
    /// <remarks> 
    /// This method can be used to retrieve the actual <see cref="Regex"/> instance created as a result of setting the mask on a <see cref="TextBox"/>. 
    /// </remarks> 
    /// <param name="textBox"> 
    /// The <see cref="TextBox"/> whose mask expression is to be retrieved. 
    /// </param> 
    /// <returns> 
    /// The mask expression as an instance of <see cref="Regex"/>, or <see langword="null"/> if no mask has been applied to <paramref name="textBox"/>. 
    /// </returns> 
    public static Regex GetMaskExpression(TextBox textBox) 
    { 
     if (textBox == null) 
     { 
      throw new ArgumentNullException("textBox"); 
     } 

     return textBox.GetValue(MaskExpressionProperty) as Regex; 
    } 

    private static void SetMaskExpression(TextBox textBox, Regex regex) 
    { 
     textBox.SetValue(_maskExpressionPropertyKey, regex); 
    } 

    private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     var textBox = dependencyObject as TextBox; 
     var mask = e.NewValue as string; 
     textBox.PreviewTextInput -= textBox_PreviewTextInput; 
     textBox.PreviewKeyDown -= textBox_PreviewKeyDown; 
     DataObject.RemovePastingHandler(textBox, Pasting); 
     DataObject.RemoveCopyingHandler(textBox, NoDragCopy); 
     CommandManager.RemovePreviewExecutedHandler(textBox, NoCutting); 

     if (mask == null) 
     { 
      textBox.ClearValue(MaskProperty); 
      textBox.ClearValue(MaskExpressionProperty); 
     } 
     else 
     { 
      textBox.SetValue(MaskProperty, mask); 
      SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace)); 
      textBox.PreviewTextInput += textBox_PreviewTextInput; 
      textBox.PreviewKeyDown += textBox_PreviewKeyDown; 
      DataObject.AddPastingHandler(textBox, Pasting); 
      DataObject.AddCopyingHandler(textBox, NoDragCopy); 
      CommandManager.AddPreviewExecutedHandler(textBox, NoCutting); 
     } 
    } 

    private static void NoCutting(object sender, ExecutedRoutedEventArgs e) 
    { 
     if (e.Command == ApplicationCommands.Cut) 
     { 
      e.Handled = true; 
     } 
    } 

    private static void NoDragCopy(object sender, DataObjectCopyingEventArgs e) 
    { 
     if (e.IsDragDrop) 
     { 
      e.CancelCommand(); 
     } 
    } 

    private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     string passHex = (string)textBox.GetValue(PassColorProperty); 
     string failHex = (string)textBox.GetValue(FailColorProperty); 
     Color passColor = Extensions.ToColorFromHex(passHex); 
     Color failColor = Extensions.ToColorFromHex(failHex); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     var proposedText = GetProposedText(textBox, e.Text); 

     if (!maskExpression.IsMatch(proposedText)) 
     { 
      textBox.Background = new SolidColorBrush(failColor); 
     } 
     else 
     { 
      textBox.Background = new SolidColorBrush(passColor); 
     } 
    } 

    private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     string passHex = (string)textBox.GetValue(PassColorProperty); 
     string failHex = (string)textBox.GetValue(FailColorProperty); 
     Color passColor = Extensions.ToColorFromHex(passHex); 
     Color failColor = Extensions.ToColorFromHex(failHex); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     string proposedText = null; 

     //pressing space doesn't raise PreviewTextInput, reasons here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/446ec083-04c8-43f2-89dc-1e2521a31f6b?prof=required 
     if (e.Key == Key.Space) 
     { 
      proposedText = GetProposedText(textBox, " "); 
     } 
     // Same story with backspace 
     else if (e.Key == Key.Back) 
     { 
      proposedText = GetProposedTextBackspace(textBox); 
     } 

     if (proposedText != null && !maskExpression.IsMatch(proposedText)) 
     { 
      textBox.Background = new SolidColorBrush(failColor); 
     } 
     else 
     { 
      textBox.Background = new SolidColorBrush(passColor); 
     } 

    } 

    private static void Pasting(object sender, DataObjectPastingEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 
     var maskExpression = GetMaskExpression(textBox); 

     string passHex = (string)textBox.GetValue(PassColorProperty); 
     string failHex = (string)textBox.GetValue(FailColorProperty); 
     Color passColor = Extensions.ToColorFromHex(passHex); 
     Color failColor = Extensions.ToColorFromHex(failHex); 

     if (maskExpression == null) 
     { 
      return; 
     } 

     if (e.DataObject.GetDataPresent(typeof(string))) 
     { 
      var pastedText = e.DataObject.GetData(typeof(string)) as string; 
      var proposedText = GetProposedText(textBox, pastedText); 

      if (!maskExpression.IsMatch(proposedText)) 
      { 
       textBox.Background = new SolidColorBrush(failColor); 
      } 
      else 
      { 
       textBox.Background = new SolidColorBrush(passColor); 
      } 
     } 
     else 
     { 
      textBox.Background = new SolidColorBrush(failColor); 
     } 
    } 

    private static string GetProposedTextBackspace(TextBox textBox) 
    { 
     var text = GetTextWithSelectionRemoved(textBox); 
     if (textBox.SelectionStart > 0) 
     { 
      text = text.Remove(textBox.SelectionStart - 1, 1); 
     } 

     return text; 
    } 


    private static string GetProposedText(TextBox textBox, string newText) 
    { 
     var text = GetTextWithSelectionRemoved(textBox); 
     text = text.Insert(textBox.CaretIndex, newText); 

     return text; 
    } 

    private static string GetTextWithSelectionRemoved(TextBox textBox) 
    { 
     var text = textBox.Text; 

     if (textBox.SelectionStart != -1) 
     { 
      text = text.Remove(textBox.SelectionStart, textBox.SelectionLength); 
     } 
     return text; 
    } 
} 

चलाने के लिए,, यहाँ समझाया, Silverlight/WPF sets ellipse with hexadecimal colour यहाँ दिखाया गया है : http://www.wiredprairie.us/blog/index.php/archives/659

कोड के मामले में नीचे है वेबसाइट कभी ले जाया जाता है:

public static class Extensions 
{ 
    public static void SetFromHex(this Color c, string hex) 
    { 
     Color c1 = ToColorFromHex(hex); 

     c.A = c1.A; 
     c.R = c1.R; 
     c.G = c1.G; 
     c.B = c1.B; 
    } 

    public static Color ToColorFromHex(string hex) 
    { 
     if (string.IsNullOrEmpty(hex)) 
     { 
      throw new ArgumentNullException("hex"); 
     } 

     // remove any "#" characters 
     while (hex.StartsWith("#")) 
     { 
      hex = hex.Substring(1); 
     } 

     int num = 0; 
     // get the number out of the string 
     if (!Int32.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out num)) 
     { 
      throw new ArgumentException("Color not in a recognized Hex format."); 
     } 

     int[] pieces = new int[4]; 
     if (hex.Length > 7) 
     { 
      pieces[0] = ((num >> 24) & 0x000000ff); 
      pieces[1] = ((num >> 16) & 0x000000ff); 
      pieces[2] = ((num >> 8) & 0x000000ff); 
      pieces[3] = (num & 0x000000ff); 
     } 
     else if (hex.Length > 5) 
     { 
      pieces[0] = 255; 
      pieces[1] = ((num >> 16) & 0x000000ff); 
      pieces[2] = ((num >> 8) & 0x000000ff); 
      pieces[3] = (num & 0x000000ff); 
     } 
     else if (hex.Length == 3) 
     { 
      pieces[0] = 255; 
      pieces[1] = ((num >> 8) & 0x0000000f); 
      pieces[1] += pieces[1] * 16; 
      pieces[2] = ((num >> 4) & 0x000000f); 
      pieces[2] += pieces[2] * 16; 
      pieces[3] = (num & 0x000000f); 
      pieces[3] += pieces[3] * 16; 
     } 
     return Color.FromArgb((byte)pieces[0], (byte)pieces[1], (byte)pieces[2], (byte)pieces[3]); 
    } 

} 
-1

एक और संभावित समाधान "मास्कड टेक्स्टबॉक्स" wpf कार्यान्वयन का उपयोग करना है जो "मास्कड टेक्स्टक्वाइडर" वर्ग का उपयोग करके Winforms से "मास्कड टेक्स्टबॉक्स" द्वारा उपयोग किया जाता है।

दो संभावित समाधानों यहां पाए जाते हैं ->https://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox और यहाँ ->http://marlongrech.wordpress.com/2007/10/28/masked-textbox/

संबंधित मुद्दे