2017-09-30 3 views
1

표준 WPF RichTextBox 컨트롤을 사용하고 있습니다.'배경'속성이 텍스트 서식 지정에 유효하지 않습니다.

나는 성공적 전경 색상을 설정하지만, 배경 색상을 설정하면 다음과 오류 제공 할 수 있습니다 내가 사용하고

// SUCCESS 
this.rtfDocument.Selection.ApplyPropertyValue(
    System.Windows.Controls.RichTextBox.ForegroundProperty, 
    System.Windows.Media.Brushes.Red); 

// ERROR 
this.rtfDocument.Selection.ApplyPropertyValue(
    System.Windows.Controls.RichTextBox.BackgroundProperty, 
    System.Windows.Media.Brushes.Blue); 

: 여기

System.ArgumentException: ''Background' property is not valid for text formatting.'

내가 함께 테스트있어 코드를 System.Windows.Media 네임 스페이스 브러쉬 다른 Stackoverflow 질문 언급.

편집 :

// SUCCESS 
var f = this.rtfDocument.Selection.GetPropertyValue(
    System.Windows.Controls.RichTextBox.ForegroundProperty); 

// ERROR 
var b = this.rtfDocument.Selection.GetPropertyValue(
    System.Windows.Controls.RichTextBox.BackgroundProperty); 

아마 오류가 실제 재산 어떻게 든 그 자체로 할 것입니다 :

은 흥미롭게도 배경색을 받고이 오류가 발생합니다?

답변

2

TextRange.ApplyPropertyValue 메서드는 속성 값을 RichTextBox 자체가 아니라 문서 요소에 적용합니다.

그래서 설정하지를 RichTextBox 속성 대신 TextElement의 속성 :

rtfDocument.Selection.ApplyPropertyValue(
    System.Windows.Documents.TextElement.ForegroundProperty, 
    System.Windows.Media.Brushes.Red); 

rtfDocument.Selection.ApplyPropertyValue(
    System.Windows.Documents.TextElement.BackgroundProperty, 
    System.Windows.Media.Brushes.Blue);