简体   繁体   中英

WPF Listview item highlight by their first character

I have a listview and it binds customer datas from database. I want to be focused by their first character. For example, when I click on to "B" from keyboard then, highlight focus must go to customers whose names' first character is "B". Do you have any idea? My listview's XAML is below.

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

You could use the code here and instead of the textbox _TextChanged event use the Window_Keydown event to capture the Key pressed.

Instead of this:

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

Use this:

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

When the ListView gots focus, you can Attach an Behavior to the Listview like this:

( System.Windows.Interactivity needs to be referenced) 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>

Behavior:

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

            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM