2014-07-22 6 views
3

WPF TextBlock에서 선택한 텍스트의 배경을 강조 표시하거나 설정하려고합니다. 2 개의 텍스트 파일을 메모리에로드하고 diff를 완료 한 다음 WPF 앱에서 디플레이하고 싶다고 가정 해 보겠습니다. 각 줄을 반복 한 다음 텍스트를 텍스트 블록에 추가하고 삭제, 삽입 또는 동일 텍스트를 기반으로 색상을 변경한다고 가정 해보십시오.WPF TextBlock의 텍스트 강조 표시

for (int i = 0; i < theDiffs.Count; i++) 
     { 
      switch (theDiffs[i].operation) 
      { 
       case Operation.DELETE: 
        // set color to red on Source control version TextBlock 
        break; 

       case Operation.INSERT: 
        WorkspaceVersion.AppendText(theDiffs[i].text); 
        // set the background color (or highlight) of appended text to green 
        break; 

       case Operation.EQUAL: 
        WorkspaceVersion.AppendText(theDiffs[i].text); 
        // Set the background color (highlight) of appended text to yellow 
        break; 

       default: 
        throw new ArgumentOutOfRangeException(); 
      } 
     } 

답변

5

당신은 TextBlock InlinesRun 인라인 요소를 추가 할 수 있습니다. 예 : "WorkspaceVersion"이 TextBlock 인 경우 :

case Operation.INSERT: 
    // set the background color (or highlight) of appended text to green 
    string text = theDiffs[i].text; 
    Brush background = Brushes.Green; 
    var run = new Run { Text = text, Background = background }; 
    WorkspaceVersion.Inlines.Add(run); 
break; 
+0

브릴리언트! 고맙습니다! –