WPF list selection problem

I have a list (in advanced selection mode and synchronized with the current item) and a text box. The text box allows the user to enter search criteria. In the TextChanged event from the list, I match the search criteria with the names listviewitems in the list and set the selected index accordingly. This is done both for highlighting and for a simple selection point to select additional points. See code below:

    void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {
            TextBox textBox = (TextBox)sender;
            if (textBox != null)
            {
                string text = textBox.Text;
                if (text != string.Empty)
                {
                    for (int i = 0; i < listViewPerson.Items.Count; i++)
                    {
                        Person person = (Person)listViewPerson.Items[i];
                        if (person != null)
                        {
                            if (person .Name.StartsWith(text, StringComparison.OrdinalIgnoreCase))
                            {
                                listViewPerson.SelectedIndex = i;
                                listViewPerson.ScrollIntoView(routePoint);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    listViewPerson.SelectedIndex = -1;
                }
            }
        }
        catch (Exception Caught)
        {
            Log.AddExceptionEntry(this, "Could not search ", Caught, Log.Target.All,
                Log.EntryType.Error);
        }
    }

Finding and selecting jobs is wonderful. On the first attempt, the user can select the second point (the first is already selected by the search) and multi-select from the first without problems.

, 2- "" ( ), ( index ).

, reset correcty. , selectedindex - .

listViewTrainServiceHelperPattern.SelectedItems.Clear(), SelectedValue (object) SelectedIndex, . - , ?

+3
1

wpf treeview, , IsSelected , . ListViewItem, ListView.ItemContainerGenerator.ContainerFromItem(newSelectedItem), focus(). , .

0

Source: https://habr.com/ru/post/1754965/


All Articles