C# 如何禁用第一次输入的标记
我创建了一个可编辑的组合框,用于搜索(过滤),比如谷歌。我正在使用ActionHandler“KeyUp”,第一个输入被突出显示并覆盖。如何禁用覆盖或高亮显示C# 如何禁用第一次输入的标记,c#,wpf,combobox,C#,Wpf,Combobox,我创建了一个可编辑的组合框,用于搜索(过滤),比如谷歌。我正在使用ActionHandler“KeyUp”,第一个输入被突出显示并覆盖。如何禁用覆盖或高亮显示 private void CbInKuLi_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) { CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultVi
private void CbInKuLi_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(cbInKuLi.ItemsSource);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(cbInKuLi.Text)) return true;
else
{
DeKreditor x = (DeKreditor)o;
string filterText = cbInKuLi.Text;
if (x.Nummer.ToLowerInvariant().Contains(filterText)
|| (!string.IsNullOrWhiteSpace(x.Firma) && x.Firma.ToLowerInvariant().Contains(filterText))
|| (!string.IsNullOrWhiteSpace(x.Vorname) && x.Vorname.ToLowerInvariant().Contains(filterText))
|| (!string.IsNullOrWhiteSpace(x.Name) && x.Name.ToLowerInvariant().Contains(filterText)))
return true;
else
return false;
}
});
itemsViewOriginal.Refresh();
cbInKuLi.IsDropDownOpen = true;
}
XAML:
由于将IsDropDownOpen设置为true而导致文本高亮显示。 可编辑组合框在打开时会自动选择文本,因此您可以从组合框的模板中按其名称获取文本框,然后在文本末尾将选择长度设置为零
private void CbInKuLi_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(cbInKuLi.ItemsSource);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(cbInKuLi.Text)) return true;
else
{
Model x = (Model)o;
string filterText = cbInKuLi.Text;
if (x.Text.ToLowerInvariant().Contains(filterText))
return true;
else
return false;
}
});
itemsViewOriginal.Refresh();
cbInKuLi.IsDropDownOpen = true;
var textbox = (TextBox)cbInKuLi.Template.FindName("PART_EditableTextBox", cbInKuLi);
textbox.Select(textbox.Text.Length, textbox.Text.Length);
}
更新:
从注释中,您可以将最后一行替换为下一行,这比原始行更好:
textbox.CaretIndex = textbox.Text.Length;
textbox.CaretIndex=textbox.Text.Length
也可以。@user3632179不客气,请将其标记为“回答并向上投票;”
textbox.CaretIndex = textbox.Text.Length;