如何檢測WPF RichTextBox中光標位置的當前文本格式?從WPF RichTextBox獲取FontWeight/FontStyle/TextDecorations
3
A
回答
0
嘗試以下其中RTB是RichTextBox的代碼:
TextRange tr = new TextRange(rtb.Selection.Start, rtb.Selection.End);
object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
3
我會使用,而不是選擇開始和結束的CaretPosition,彷彿RichTextBox中實際上有一個選擇跨越格式化你的多個區域會得到DependencyProperty.UnsetValue。
TextRange tr = new TextRange(rtb.CaretPosition, rtb.CaretPosition); object oFont = tr.GetPropertyValue(Run.FontFamilyProperty);
3
該主題的作者還詢問了有關TextDecorations的情況,您未提供示例代碼及其使用的不同。我張貼此作爲進一步溶液:
var obj = _myText.GetPropertyValue(Inline.TextDecorationsProperty);
if (obj == DependencyProperty.UnsetValue)
IsTextUnderline = false;// mixed formatting
if (obj is TextDecorationCollection)
{
var objProper = obj as TextDecorationCollection;
if (objProper.Count > 0)
IsTextUnderline = true; // all underlined
else
IsTextUnderline = false; // nothing underlined
}
1
這裏是確定fontWeight設置,FontStyle,TextDecorations(刪除線和下劃線)和的Super和下標中的溶液。
TextRange textRange = new TextRange(rtb.Selection.Start, rtb.Selection.End);
bool IsTextUnderline = false;
bool IsTextStrikethrough = false;
bool IsTextBold = false;
bool IsTextItalic = false;
bool IsSuperscript = false;
bool IsSubscript = false;
// determine underline property
if (textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Strikethrough))
IsTextStrikethrough = true; // all underlined
else if (textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Underline))
IsTextUnderline = true; // all strikethrough
// determine bold property
if (textRange.GetPropertyValue(Inline.FontWeightProperty).Equals(FontWeights.Bold))
IsTextBold = true; // all bold
// determine if superscript or subscript
if (textRange.GetPropertyValue(Inline.BaselineAlignmentProperty).Equals(BaselineAlignment.Subscript))
IsSubscript = true; // all subscript
else if (textRange.GetPropertyValue(Inline.BaselineAlignmentProperty).Equals(BaselineAlignment.Superscript))
IsSuperscript = true; // all superscript
相關問題
- 1. WPF RichTextBox - 獲取當前行
- 2. 獲取WPF Richtextbox中TextPointer的XY座標
- 3. WPF RichTextBox獲取選定元素
- 4. 獲取從RichTextBox的在C#
- 5. 如何從RichTextBox獲取RTF
- 6. 從Silverlight RichTextBox獲取行數?
- 7. 如何從RichTextBox中獲取WPF中的所有文本
- 8. 獲取RichTextBox的
- 9. WPF Richtextbox FontFace/FontSize
- 10. WPF RichTextBox和'\ t'
- 11. WPF RichTextBox ApplyPropertyValue
- 12. WPF RichTextBox GetCharacterRect
- 13. WPF RichTextBox RTL
- 14. WPF richTextBox問題
- 15. WPF RichTextBox bullet
- 16. 當單元格爲RichTextBox時從DataGrid獲取RichTextBox
- 17. 從Silverlight獲取文本顏色RichTextBox
- 18. 從RichTextBox獲取文本的一部分
- 19. 從RichTextBox獲取滾動位置?
- 20. WPF,RichTextBox在獲取光標位置的正確textproperties問題
- 21. 使用GetLineStartPosition獲取WPF RichTextBox中行的結尾
- 22. WPF RichTextBox - 在當前插入位置獲取整個字
- 23. 文本WPF RichTextBox的獲取文件塊改變
- 24. WPF中的RichTextBox - 獲取最後一個文本塊
- 25. WPF RichTextBox SelectionChanged性能
- 26. RichTextBox的WPF結合
- 27. RichTextBox中的行,WPF
- 28. 刪除RichTextBox的WPF
- 29. 如何獲取richtextbox行?
- 30. 獲取RichTextBox的底線VB.NET
最困難的是沒有回答看到我的答案! – msfanboy 2010-08-10 21:22:24