0

我有一个绑定客户信息的列表视图。另外,我有一个在列表视图中提供搜索的文本框。如果文本框的键入字符或符号与数据列表项匹配,则它会突出显示匹配项。关于这一点,我想计算匹配的项目数。但是每次搜索计数都是“0”。当我调试它时,foreach 循环中的计数为真,但在 TxtSearch_PreviewKeyDown 中它始终为零。请看一看。我怎样才能找到匹配的项目数量?

 private int highlightcount;
    public int highlightCount
    {
        get;
        set;
    }       
    private void FindListViewItem(DependencyObject obj)
    {
        try
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                ListViewItem lv = obj as ListViewItem;
                if (lv != null)
                {
                    HighlightText(lv);
                }
                else
                    FindListViewItem(VisualTreeHelper.GetChild(obj as DependencyObject, i));
            }
        }
        catch
        {
            MessageBox.Show("Bevor Sie die Suche Stauten, wählen Sie bitte eine Adresse in der linken Spalte aus.");
        }
    }
    /// <summary>
    /// Method for highlighting matched listview item
    /// </summary>
    /// <param name="itx"></param>
   public void HighlightText(Object itx)
    {
        try
        {
            if (itx != null)
            {
                if (itx is TextBlock)
                {
                    Regex regex = new Regex("(" + TxtSearch.Text + ")", RegexOptions.IgnoreCase);
                    TextBlock tb = itx as TextBlock;
                    if (TxtSearch.Text.Length == 0)
                    {
                        string str = tb.Text;
                        tb.Inlines.Clear();
                        tb.Inlines.Add(str);
                        return;
                    }
                    string[] substrings = regex.Split(tb.Text);
                    tb.Inlines.Clear();

                    highlightCount = 0;
                    foreach (var item in substrings)
                    {
                        if (regex.Match(item).Success)
                        {                                
                            Run runx = new Run(item);
                            runx.Background = Brushes.Lime;                               
                            tb.Inlines.Add(runx);
                            highlightCount++;

                            if (tb.IsMouseOver)
                            {
                                tb.IsEnabled = false;                                   
                            }
                        }
                        else
                        {
                            tb.Inlines.Add(item);
                            tb.IsEnabled = false;                             
                        }                            
                    }                        
                    return ;
                } 
                else
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(itx as DependencyObject); i++)
                    {
                        HighlightText(VisualTreeHelper.GetChild(itx as DependencyObject, i));
                    }
                }
            }
        }
        catch
        {
            MessageBox.Show("Suche Error");
        }
    }       
    private void TxtSearch_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (TxtSearch.Text.Length > 1 && e.Key==Key.Enter)
        {
            Mouse.OverrideCursor = Cursors.Wait;
            ListControl lc = getactivListview();
            FindListViewItem(lc);               
            Mouse.OverrideCursor = null;
            MessageBox.Show(highlightCount.ToString());
            FocusManager.SetFocusedElement(this, TxtSearch);  
        }
4

1 回答 1

0

尝试highlightCount = 0;从您的 HighlightText 方法中删除并将其添加到TxtSearch_PreviewKeyDownEventhandler(上ListControl lc = getactivListview();或下MessageBox.Show(highlightCount.ToString());

于 2012-12-13T07:05:38.890 回答