2012-11-12 59 views
0

我有一個listview綁定客戶信息。我通過在文本框中鍵入來突出顯示列表視圖項目。例如,當我在文本框中輸入「PET」時,它會突出顯示列表視圖項中的「PET」。它的作品和亮點。WPF Listview Textbox Highlight

但之後,當我點擊突出顯示的項目時,它會給出錯誤。但是,當我點擊它的工作列表視圖項目中的空閒位置時,這很有趣。例如,它強調了PETER HEINZ。如果我點擊PETER或HEINZ就會報錯。但是,如果我點擊彼得海因茨之間的空間它的作品。這是什麼錯誤?錯誤消息是

System.InvalidOperationException未處理 Message =「'System.Windows.Documents.Run'不是Visual或Visual3D。」

的源代碼是下面:

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

    } 
+0

你試過調試你的代碼嗎?你檢查了哪一行顯示錯誤? [你有什麼嘗試?](http://whathaveyoutried.com) – Blachshma

+0

「它給錯誤」什麼錯誤?簡單地告訴我們,異常類型可以讓我們更輕鬆地幫助你。 –

+0

@Blachshma我試過調試,看起來像線。但只有當您點擊突出顯示的文本塊時纔會出現錯誤。這就是爲什麼我不能理解它。 – Isi

回答

0

這裏的一個revisement。你可以將它粘貼在你的上面,看看它在你的應用程序中是否更好?

關鍵點:

  • 額外保護不讓非可視化對象使它的GetChild遞歸
  • 新增支票TextBox1的是空
  • 簡單:移動tb.inlines.clear減少代碼repitition
  • 瑣碎:倒空ITX檢查,以減少嵌套

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

。@ bill塔爾貝爾,當我申請你的代碼,然後所有的listview項目消失.. – Isi

+0

對不起,提供一些安全檢查是我可以做的最好的提供的信息。如果您想直接調試支持,我建議提供一個小型工作示例。 –