मैं NumericTextBox
से परिचित नहीं हूं, लेकिन यहां एक साधारण सी #/एक्सएएमएल कार्यान्वयन है जो केवल अंक और दशमलव वर्ण की अनुमति देता है।
यह सब OnKeyDown
ईवेंट ओवरराइड करता है; दबाए जा रहे कुंजी के आधार पर, यह घटना को TextBox
कक्षा तक पहुंचने की इजाजत देता है या अनुमति देता है।
मुझे ध्यान रखना चाहिए कि यह कार्यान्वयन विंडोज स्टोर ऐप्स के लिए है - मेरा मानना है कि आपका प्रश्न उस प्रकार के ऐप के बारे में है, लेकिन मैं 100% निश्चित नहीं हूं।
public class MyNumericTextBox : TextBox
{
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
HandleKey(e);
if (!e.Handled)
base.OnKeyDown(e);
}
bool _hasDecimal = false;
private void HandleKey(KeyRoutedEventArgs e)
{
switch (e.Key)
{
// allow digits
// TODO: keypad numeric digits here
case Windows.System.VirtualKey.Number0:
case Windows.System.VirtualKey.Number1:
case Windows.System.VirtualKey.Number2:
case Windows.System.VirtualKey.Number3:
case Windows.System.VirtualKey.Number4:
case Windows.System.VirtualKey.Number5:
case Windows.System.VirtualKey.Number6:
case Windows.System.VirtualKey.Number7:
case Windows.System.VirtualKey.Number8:
case Windows.System.VirtualKey.Number9:
e.Handled = false;
break;
// only allow one decimal
// TODO: handle deletion of decimal...
case (Windows.System.VirtualKey)190: // decimal (next to comma)
case Windows.System.VirtualKey.Decimal: // decimal on key pad
e.Handled = (_hasDecimal == true);
_hasDecimal = true;
break;
// pass various control keys to base
case Windows.System.VirtualKey.Up:
case Windows.System.VirtualKey.Down:
case Windows.System.VirtualKey.Left:
case Windows.System.VirtualKey.Right:
case Windows.System.VirtualKey.Delete:
case Windows.System.VirtualKey.Back:
case Windows.System.VirtualKey.Tab:
e.Handled = false;
break;
default:
// default is to not pass key to base
e.Handled = true;
break;
}
}
}
यहां कुछ नमूना एक्सएएमएल है। ध्यान दें कि यह प्रोजेक्ट नेमस्पेस में MyNumericTextBox
मानता है।
<StackPanel Background="Black">
<!-- custom numeric textbox -->
<local:MyNumericTextBox />
<!-- normal textbox -->
<TextBox />
</StackPanel>
स्रोत
2013-10-15 02:07:13
'इनपुटस्कोप' का उपयोग टच इनपुट कीबोर्ड प्रकार के लिए किया जाता है। – BrunoLM