2013-07-12 7 views
5

मेरे पास कई टेक्स्टबॉक्स वाले विंडोज 8 स्टोर एप्लिकेशन हैं। जब मैं कीबोर्ड पर एंटर दबाता हूं तो मैं फोकस को अगले नियंत्रण में ले जाना चाहता हूं।विंडोज 8 स्टोर एप्लिकेशन में दर्ज/रिटर्न प्रेस पर अगले नियंत्रण पर जाएं

मैं यह कैसे कर सकता हूं?

धन्यवाद

उत्तर

5

आप KeyDown/KeyUp अपने बक्सें पर घटनाओं संभाल कर सकते हैं (चाहे आप शुरुआत में अगले एक या कुंजी दबाएँ के अंत में जाना चाहते हैं पर निर्भर करता है)।

उदाहरण XAML:

<TextBox KeyUp="TextBox_KeyUp" /> 

कोड के पीछे:

private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e) 
    { 
     TextBox tbSender = (TextBox)sender; 

     if (e.Key == Windows.System.VirtualKey.Enter) 
     { 
      // Get the next TextBox and focus it. 

      DependencyObject nextSibling = GetNextSiblingInVisualTree(tbSender); 
      if (nextSibling is Control) 
      { 
       // Transfer "keyboard" focus to the target element. 
       ((Control)nextSibling).Focus(FocusState.Keyboard); 
      } 
     } 
    } 

GetNextSiblingInVisualTree() सहायक विधि के लिए कोड सहित पूर्ण उदाहरण कोड: https://github.com/finnigantime/Samples/tree/master/examples/Win8Xaml/TextBox_EnterMovesFocusToNextControl

ध्यान दें कि फोकस बुला() के साथ FocusState.Keyboard उन तत्वों के चारों ओर बिंदीदार फोकस रेक्ट दिखाता है जिनके नियंत्रण टेम्पलेट में ऐसे रेक्ट हैं (ई। जी। बटन)। FocusState.Pointer के साथ कॉलिंग फोकस() फोकस रेक्ट नहीं दिखाता है (आप स्पर्श/माउस का उपयोग कर रहे हैं, इसलिए आप जानते हैं कि आप किस तत्व से बातचीत कर रहे हैं)।

+0

धन्यवाद पैट्रिक। यह एक इलाज करता है। – Sun

1

मैंने "GetNextSiblingInVisualTree" फ़ंक्शन में थोड़ा सुधार किया है। यह संस्करण अगले ऑब्जेक्ट के बजाय अगले टेक्स्टबॉक्स की खोज करता है।

private static DependencyObject GetNextSiblingInVisualTree(DependencyObject origin) 
    { 
     DependencyObject parent = VisualTreeHelper.GetParent(origin); 

     if (parent != null) 
     { 
      int childIndex = -1; 
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); ++i) 
      { 
       if (origin == VisualTreeHelper.GetChild(parent, i)) 
       { 
        childIndex = i; 
        break; 
       } 
      } 

      for (int nextIndex = childIndex + 1; nextIndex < VisualTreeHelper.GetChildrenCount(parent); nextIndex++) 
      { 
       DependencyObject currentObject = VisualTreeHelper.GetChild(parent, nextIndex); 

       if(currentObject.GetType() == typeof(TextBox)) 
       { 
        return currentObject; 
       } 
      } 
     } 

     return null; 
    } 
संबंधित मुद्दे