简体   繁体   中英

WPF Listview Textbox Highlight

I have a listview which binds the customers informations. I highlight the listview items by typing in a textbox. For example, when I type "PET" into the textbox then it highlights the "PET"s in the listview items. It works and highlights.

But after that when I click on the highlighted item it gives an error. But it is interesting when I click on a free place in the listview item it works. For example, it highlighted PETER HEINZ. IF I click on to PETER or HEINZ it gives error. But, if I click on the space between PETER HEINZ it works. What a error is this? The error message is

System.InvalidOperationException was unhandled Message="'System.Windows.Documents.Run' is not a Visual or Visual3D."

The source code is below:

private void HighlightText(Object itx)
    {
 if (itx != null)
        {
            if (itx is TextBlock)
            {
                Regex regex = new Regex("(" + textBox1.Text + ")", RegexOptions.IgnoreCase);
                TextBlock tb = itx as TextBlock;
                if (textBox1.Text.Length == 0)
                {
                    string str = tb.Text;
                    tb.Inlines.Clear();
                    tb.Inlines.Add(str);
                    return;
                }
                string[] substrings = regex.Split(tb.Text);
                tb.Inlines.Clear();
                foreach (var item in substrings)
                {
                    if (regex.Match(item).Success)
                    {
                        Run runx = new Run(item);
                        runx.Background = Brushes.LightGray;
                        tb.Inlines.Add(runx);
                    }
                    else
                    {
                        tb.Inlines.Add(item);
                    }
                }
                return;
            }
            else
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
                {
                    HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
                }
            }
        }

    }

Here's a revisement. Can you paste it over yours and see if it works better in your application?

key points:

  • Added protection to not let non-visual objects make it to the GetChild recursion
  • Added check for textbox1 being empty
  • trivial: moved tb.inlines.clear to reduce code repitition
  • trivial: inverted itx null check to reduce nesting

.

private void HighlightText(Object itx)
{
    //safety checks
    if (itx == null)
        return;        
    if (String.isNullOrEmpty(textBox1.Text)
        return; //just in case the box is empty
    if (! (itx is Visual || itx is System.Windows.Media.Media3D.Visual3D)
        return;  //prevent from getting children on non-visual element

    if (itx is TextBlock)
    {
        Regex regex = new Regex("(" + textBox1.Text + ")", RegexOptions.IgnoreCase);
        TextBlock tb = itx as TextBlock;
        tb.Inlines.Clear();
        if (textBox1.Text.Length == 0)
        {
            string str = tb.Text;
            tb.Inlines.Add(str);
            return;
        }
        string[] substrings = regex.Split(tb.Text);
        foreach (var item in substrings)
        {

            if (!regex.Match(item).Success)
            {
                Run runx = new Run(item);
                runx.Background = Brushes.LightGray;
                tb.Inlines.Add(runx);
            }
            else
            {
                tb.Inlines.Add(item);
            }
        }
        return;
    }
    else
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
        {
            HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
        }
    }
}

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