當ListView的GOTS焦點時,您可以將一個行爲到ListView這樣的:
(System.Windows.Interactivity
需求被引用) Xaml:
<ListView SelectionMode="Multiple">
<ListViewItem>A Company 1</ListViewItem>
<ListViewItem>A Company 2</ListViewItem>
<ListViewItem>A Company 3</ListViewItem>
<ListViewItem>B Company 1</ListViewItem>
<ListViewItem>B Company 2</ListViewItem>
<ListViewItem>C Company 1</ListViewItem>
<ListViewItem>C Company 2</ListViewItem>
<ListViewItem>C Company 3</ListViewItem>
<ListViewItem>D Company 1</ListViewItem>
<ListViewItem>D Company 2</ListViewItem>
<ListViewItem>E Company 1</ListViewItem>
<ListViewItem>F Company 1</ListViewItem>
<i:Interaction.Behaviors>
<ListBoxHighlighting:ListViewBehavior />
</i:Interaction.Behaviors>
</ListView>
行爲:
public class ListViewBehavior : Behavior<ListView>
{
protected override void OnAttached()
{
AssociatedObject.KeyDown += OnKeyDown;
}
protected override void OnDetaching()
{
AssociatedObject.KeyDown -= OnKeyDown;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
var key = e.Key.ToString();
AssociatedObject.SelectedItems.Clear();
foreach (var item in AssociatedObject.Items)
{
var i = (item as ListViewItem).Content;
if(i.ToString().StartsWith(key))
{
AssociatedObject.SelectedItems.Add(item);
}
}
}
}
http://www.codeproject.com/Articles/235203/WPF-Highlight-ListView-items OR http://www.codeproject.com/Articles/409275/Highlight-Searched -Text-in-WPF-ListView – Sandeep
@Sandeep我不會使用文本框。我只是想在鍵盤上打字。沒有到任何地方.. – Isi
只是想知道更多,如果你不打算使用文本框,你會怎麼做,如果你想取消鍵入的關鍵字?你會只是按下「Backspace」並假定之前輸入的字符已經從搜索算法中拉出來了嗎? – Sandeep