<TextBlock FontSize="20"> <Run Text="Hel" /><Run Foreground="Red" Text="lo " /><Run Text="Word" /> </TextBlock>需要注意的是每个Run之间不要换行,如果换行的话,每个Run之间会有间隙,看起来像增加了空格。通过这种方式实现查找结果中高亮关键字,需要把查找结果拆分成三部分,然后绑定到Run元素的Text属性,或者在后台代码中使用TextBlock的Inlines属性添加Run元素
// 堆代码 duidaima.com textBlock1.Inlines.Add(new Run("hel")); textBlock1.Inlines.Add(new Run("lo ") { Foreground=new SolidColorBrush(Colors.Red)}); textBlock1.Inlines.Add(new Run("world"));
这种方法虽然可以达到效果,但显然与MVVM的思想不符。接下来本文介绍一种通过附加属性实现TextBlock中指定内容高亮。
先看一下效果:
public class ColoredLettering { public static void SetColorStart(TextBlock textElement, int value) { textElement.SetValue(ColorStartProperty, value); } public static int GetColorStart(TextBlock textElement) { return (int)textElement.GetValue(ColorStartProperty); } // 堆代码 duidaima.com // Using a DependencyProperty as the backing store for ColorStart. This enables animation, styling, binding, etc... public static readonly DependencyProperty ColorStartProperty = DependencyProperty.RegisterAttached("ColorStart", typeof(int), typeof(ColoredLettering), new FrameworkPropertyMetadata(0, OnColorStartChanged)); private static void OnColorStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TextBlock textBlock = d as TextBlock; if (textBlock != null) { if (e.NewValue == e.OldValue) return; if (e.NewValue is int) { int count = GetColorLength(textBlock); Brush brush = GetForeColor(textBlock); if ((int)e.NewValue <= 0 || count <= 0 || brush == TextBlock.ForegroundProperty.DefaultMetadata.DefaultValue) return; if (textBlock.TextEffects.Count != 0) { textBlock.TextEffects.Clear(); } TextEffect textEffect = new TextEffect() { Foreground = brush, PositionStart = (int)e.NewValue, PositionCount = count }; textBlock.TextEffects.Add(textEffect); } } } public static void SetColorLength(TextBlock textElement, int value) { textElement.SetValue(ColorLengthProperty, value); } public static int GetColorLength(TextBlock textElement) { return (int)textElement.GetValue(ColorLengthProperty); } // Using a DependencyProperty as the backing store for ColorStart. This enables animation, styling, binding, etc... public static readonly DependencyProperty ColorLengthProperty = DependencyProperty.RegisterAttached("ColorLength", typeof(int), typeof(ColoredLettering), new FrameworkPropertyMetadata(0, OnColorLengthChanged)); private static void OnColorLengthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TextBlock textBlock = d as TextBlock; if (textBlock != null) { if (e.NewValue == e.OldValue) return; if (e.NewValue is int) { int start = GetColorStart(textBlock); Brush brush = GetForeColor(textBlock); if ((int)e.NewValue <= 0 || start <= 0 || brush == TextBlock.ForegroundProperty.DefaultMetadata.DefaultValue) return; if (textBlock.TextEffects.Count != 0) { textBlock.TextEffects.Clear(); } TextEffect textEffect = new TextEffect() { Foreground = brush, PositionStart = start, PositionCount = (int)e.NewValue }; textBlock.TextEffects.Add(textEffect); } } } public static void SetForeColor(TextBlock textElement, Brush value) { textElement.SetValue(ColorStartProperty, value); } public static Brush GetForeColor(TextBlock textElement) { return (Brush)textElement.GetValue(ForeColorProperty); } // Using a DependencyProperty as the backing store for ForeColor. This enables animation, styling, binding, etc... public static readonly DependencyProperty ForeColorProperty = DependencyProperty.RegisterAttached("ForeColor", typeof(Brush), typeof(ColoredLettering), new PropertyMetadata(TextBlock.ForegroundProperty.DefaultMetadata.DefaultValue, OnForeColorChanged)); private static void OnForeColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TextBlock textBlock = d as TextBlock; if (textBlock != null) { if (e.NewValue == e.OldValue) return; if (e.NewValue is Brush) { int start = GetColorStart(textBlock); int count = GetColorLength(textBlock); if (start <= 0 || count <= 0) return; if (textBlock.TextEffects.Count != 0) { textBlock.TextEffects.Clear(); } TextEffect textEffect = new TextEffect() { Foreground = (Brush)e.NewValue, PositionStart = start, PositionCount = count }; textBlock.TextEffects.Add(textEffect); } } } }调用时只需在TextBlock指定需要高亮内容的开始位置,内容长度以及高亮颜色即可。
<TextBlock local:ColoredLettering.ColorLength="{Binding Count}" local:ColoredLettering.ColorStart="{Binding Start}" local:ColoredLettering.ForeColor="{Binding ForeColor}" FontSize="20" Text="Hello World" />