2010-09-07 47 views
1

我有这样的代码,增加了虚线在文本框中的文本下:如何格式化WPF中的特定文本?

 // Create an underline text decoration. Default is underline. 
    TextDecoration myUnderline = new TextDecoration(); 

    // Create a linear gradient pen for the text decoration. 
    Pen myPen = new Pen(); 
    myPen.Brush = new LinearGradientBrush(Colors.White, Colors.White, new Point(0, 0.5), new Point(1, 0.5)); 
    myPen.Brush.Opacity = 0.5; 
    myPen.Thickness = 1.0; 
    myPen.DashStyle = DashStyles.Dash; 
    myUnderline.Pen = myPen; 
    myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended; 

    // Set the underline decoration to a TextDecorationCollection and add it to the text block. 
    TextDecorationCollection myCollection = new TextDecorationCollection(); 
    myCollection.Add(myUnderline); 
    PasswordSendMessage.TextDecorations = myCollection; 

我的问题是,我需要在文本中只有最后6个字符被格式化!

任何想法我怎么能做到这一点?

回答

0

你可以进行数据绑定您的文字文本框的内联属性,使转换器与一个单独的运行建立运行集合的最后6个字符将您的装饰品

+0

'TextBlock.Inlines'不是一个依赖属性,所以不幸的是你不能使用数据绑定绑定它。 – Quartermeister 2010-09-07 12:35:38

+0

哦,应该看了起来。在这种情况下,人们总是可以走附加的道具路线。 – MrDosu 2010-09-08 07:11:31

1

而不是设置在整个TextBlock的财产,创建最后六个字符一个TextRange和格式适用于:

var end = PasswordSendMessage.ContentEnd; 
var start = end.GetPositionAtOffset(-6) ?? PasswordSendMessage.ContentStart; 
var range = new TextRange(start, end); 
range.ApplyPropertyValue(Inline.TextDecorationsProperty, myCollection); 

如果PasswordSendMessage是一个文本框,而不是一个TextBlock,那么你就不能使用丰富的文字是这样的。您可以使用RichTextBox,在这种情况下,此技术可以工作,但您需要使用PasswordSendMessage.Document.ContentEndPasswordSendMessage.Document.ContentStart而不是PasswordSendMessage.ContentEndPasswordSendMessage.ContentStart