C# 如何防止可编辑组合框中的键盘导航
我有一个WPF组合框,它有一个数字列表。它还有一个自定义项,它变成了一个可编辑的文本框,供用户输入自己的值 在可编辑组合框中,如果我按下项目列表中的数字,它将跳转到该数字并跳出可编辑文本框 示例:组合框包含项目“1”、“2”、“3”、“自定义”。 我单击“自定义”以输入自己的值。我按3键输入“30”,它跳到第3项 XAMLC# 如何防止可编辑组合框中的键盘导航,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我有一个WPF组合框,它有一个数字列表。它还有一个自定义项,它变成了一个可编辑的文本框,供用户输入自己的值 在可编辑组合框中,如果我按下项目列表中的数字,它将跳转到该数字并跳出可编辑文本框 示例:组合框包含项目“1”、“2”、“3”、“自定义”。 我单击“自定义”以输入自己的值。我按3键输入“30”,它跳到第3项 XAML <ComboBox x:Name="comboBox1" HorizontalAlignment="Left" Margin="115,156,0,0" Vertica
<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>
您可以尝试禁用
IsTextSearchEnabled
请参阅:我在XAML中使用了IsTextSearchEnabled=“False”,它似乎有效,但有一个副作用。当组合框变为可编辑时,它会在框中保留单词“Custom”,而不是像以前一样将其清除为空白。我试图用comboBox1.Text=string.Empty清除它;和comboBox1.SelectedItem=string.Empty;但是它不清除。Action Action=()=>{//clear Custom Text comboBox1.Text=string.Empty;};Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,操作);像这样清除文本我可以使用comboBox1.SelectedIndex=-1;而不是string.Empty,它清除了可编辑文本。
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;
}
}