2016-12-20 10 views
0

WPFのComboBoxには数字のリストがあります。また、ユーザが独自の値を入力するための編集可能なテキストボックスに変わるカスタムアイテムもあります。編集可能なComboBoxでのキーボードナビゲーションの防止方法

編集可能なコンボボックスでは、アイテムのリストにある番号を押すと、その番号にジャンプし、編集可能なテキストボックスからジャンプします。

例: ComboBoxには、項目「1」、「2」、「3」、「カスタム」があります。 私は自分の値を入力するためにカスタムをクリックします。私は "30" と入力して3を押して、それが項目にジャンプします3.

XAML

<ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="115,156,0,0" VerticalAlignment="Top" Width="53" Foreground="White"> 
    <System:String>1</System:String> 
    <System:String>2</System:String> 
    <System:String>3</System:String> 
    <System:String>Custom</System:String> 
</ComboBox> 

私はC#を使用して編集可能に静的からコンボボックスターンを作る

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    // Custom ComboBox Editable 
    if ((string)comboBox1.SelectedItem == "Custom" | comboBox1.SelectedValue == null) 
    { 
     comboBox1.IsEditable = true; 
    } 

    // Other Items Disable Editable 
    if ((string)comboBox1.SelectedItem != "Custom" && comboBox1.SelectedValue != null) 
    { 
     comboBox1.IsEditable = false; 
    } 

    // Maintain Editable ComboBox while typing 
    if (comboBox1.IsEditable == true) 
    { 
     comboBox1.IsEditable = true; 

     // Clear Custom Text 
     comboBox1.Text = string.Empty; 
    } 
} 

答えて

2

ますIsTextSearchEnabledを無効にすることができます。 IsTextSearchEnabled

+0

私はXAMLでIsTextSearchEnabled =「false」に使用し、動作しているようですが、1件の副作用があります:

を参照してください。 ComboBoxが編集可能になると、前と同じように空白を消去するのではなく、ボックスに単語 "Custom"が保持されます。私はそれをクリアしようとしましたcomboBox1.Text = string.Empty;およびcomboBox1.SelectedItem = string.Empty;それは明確ではありません。 –

+0

アクションアクション=()=> { //カスタムテキストをクリア comboBox1.Text = string.Empty; }; Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal、action); このようなテキストをクリアします – WPFUser

+1

私はcomboBox1.SelectedIndex = -1を使用することができました。 string.Emptyの代わりに編集可能なテキストをクリアしました。 –

関連する問題