2012-11-12 4 views
0

고객 정보를 바인딩하는 listview가 있습니다. 텍스트 상자에 타이핑하여 목록보기 항목을 강조 표시합니다. 예를 들어, 텍스트 상자에 "PET"를 입력하면 목록보기 항목에서 "PET"가 강조 표시됩니다. 그것은 작동하고 하이라이트.WPF Listview Textbox Highlight

하지만 강조 표시된 항목을 클릭하면 오류가 발생합니다. 그러나 그것이 작동하는 listview 항목에서 자유로운 장소를 클릭하면 흥미 롭습니다. 예를 들어 피터 하인즈가 강조 표시되었습니다. 피터 또는 하인즈를 클릭하면 오류가 발생합니다. 하지만 피터 하인즈 사이의 공간을 클릭하면 작동합니다. 이게 무슨 실수 야? 오류 메시지가

System.InvalidOperationException이 처리되지 않은 메시지 =였다이다 "비주얼 또는 Visual3D 아닌 'System.Windows.Documents.Run'."

소스 코드은 다음과 같습니다 :

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입니다. 당신의 위에 붙여 넣을 수 있고 당신의 응용 프로그램에서 더 잘 작동하는지 볼 수 있습니까?

핵심 포인트 : 감소하는 tb.inlines.clear 이동 : 비 시각적 객체가

  • 사소한 비어있는 TextBox1에 대한 GetChild 재귀
  • 추가 검사에 그것을 만들 못하게하는

    • 추가 보호 코드 repitition
    • 사소한 : 반전 ITX 널 (null) 검사는

    중첩 줄일 수 있습니다.

    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 Tarbell 코드를 적용하면 모든 목록보기 항목이 사라집니다 .. – Isi

    +0

    죄송합니다. 제공되는 정보로 할 수있는 최선의 안전 점검이 제공됩니다. 직접 디버깅 지원을 원한다면 작은 작업 샘플을 제공하는 것이 좋습니다. –