2012-11-07 103 views
0

我有一個listview,它綁定來自數據庫的客戶數據。我想通過他們的第一個人物來關注。例如,當我從鍵盤上點擊「B」時,突出顯示焦點必須發送給名稱爲「第一個字符爲」B「的客戶。你有什麼主意嗎?我的列表視圖的XAML如下。WPF Listview項目突出顯示他們的第一個字符

<ListView x:Name="datalist" ButtonBase.Click="datalist_Click" ContextMenuOpening="datalist_ContextMenuOpening" MouseDoubleClick="datalist_MouseDoubleClick" SelectionChanged="datalist_SelectionChanged" 
MouseUp="datalist_MouseUp" PreviewMouseUp="datalist_PreviewMouseUp" > 
+1

http://www.codeproject.com/Articles/235203/WPF-Highlight-ListView-items OR http://www.codeproject.com/Articles/409275/Highlight-Searched -Text-in-WPF-ListView – Sandeep

+0

@Sandeep我不會使用文本框。我只是想在鍵盤上打字。沒有到任何地方.. – Isi

+0

只是想知道更多,如果你不打算使用文本框,你會怎麼做,如果你想取消鍵入的關鍵字?你會只是按下「Backspace」並假定之前輸入的字符已經從搜索算法中拉出來了嗎? – Sandeep

回答

0

你可以使用代碼和here而不是文本框_TextChanged事件中使用Window_Keydown事件捕捉按下的鍵。

取而代之的是:

private void textboxsearch_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    FilterItems(listPerson, textBoxSearch.Text); 
} 

使用此:

private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
{ 
    FilterItems(listPerson, e.Key.ToString()); 
} 
0

當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); 
       } 

      } 
     } 
    }