简体   繁体   English

WPF Listview项目以其第一个字符突出显示

[英]WPF Listview item highlight by their first character

I have a listview and it binds customer datas from database. 我有一个listview,它绑定了数据库中的客户数据。 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". 例如,当我从键盘上单击“ B”时,突出显示焦点必须转到名称的第一个字符为“ B”的客户。 Do you have any idea? 你有什么主意吗? My listview's XAML is below. 我的列表视图的XAML在下面。

<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. 您可以在此处使用代码,而不是文本框_TextChanged事件,请使用Window_Keydown事件来捕获按下的键。

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: 当ListView成为焦点时,您可以将一个Behavior附加到Listview上,如下所示:

( System.Windows.Interactivity needs to be referenced) Xaml: (需要引用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>

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

            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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