2017-04-11 55 views
1

我正在開發一個UWP項目,其中我有UI中的多個文本框,我附加了x:Name屬性。我已將所有Textboxes與單個KeyButton_Down()事件綁定在一起。我想實現設定每個TextBox作爲NumberInputScope財產。每個文本框的XAML都有InputScope="Number"集。在後面的代碼中,我無法訪問特定Textbox的InputScope屬性。 這裏是我的代碼在代碼中的單個事件中訪問多個TextBoxes InputScope屬性

private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e) 
     { 
      TextBox tb = (TextBox)sender; 

      InputScope scope = new InputScope(); 
      InputScopeName name = new InputScopeName(); 
      name.NameValue = InputScopeNameValue.Number; 
      scope.Names.Add(name); 
      string textBoxName = tb.Name; 
      // TextBox text = new TextBox(); 

      //this line is not working for me 
      // textBoxName.InputScope = scope; 
      var state = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift); 
      var newState = (state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down; 


      if ((!char.IsControl(Convert.ToChar(e.Key)) && !char.IsDigit(Convert.ToChar(e.Key)) && (Convert.ToChar(e.Key) != '.') && !newState)) 
      { 
       e.Handled = true; 
      } 

     } 
+0

是否拋出異常? – Laith

+0

@Laith我想從(發件人)使用文本框名稱; tb.Name.Visibility是不是有 – Apoorv

+0

@Apoorv:如果你喜歡訪問某些屬性,您將不再需要的名字。 tb是你的'TextBox'鑄造的特定對象。您可以直接使用這些屬性:'tb.Visibility'。就像我在我的回答中發佈的那樣...... – WPFGermany

回答

0

我會直接用發送者(它指向你喜歡訪問特定TextBox)。

tb.InputScope = new InputScope(); 
tb.InputScope.Names.Clear(); 
tb.InputScope.Names.Add(new InputScopeNameConverter().ConvertFrom(InputScopeNameValue.Number.ToString())); 

tbTextBox鑄造發送對象...

+0

這是完美的!我只是感到困惑。謝謝你的幫助 – Apoorv

相關問題