WPF 关键字高亮 Label自定义控件

工作中要开发一个英语词典小工具,通过单词查询出的例句中相应的单词要进行高亮显示,于是就做了一个Label自定义控件。
代码如下:

HighlightLabel.cs

    public class HighlightLabel : Label
    {
        /// <summary>
        /// 高亮画笔
        /// </summary>
        public static DependencyProperty HighlightForegroundProperty =
            DependencyProperty.Register("HighlightForeground", typeof(Brush), typeof(HighlightLabel), new PropertyMetadata(new SolidColorBrush(Colors.Red), new PropertyChangedCallback(OnHighlightForegroundChanged)));

        /// <summary>
        /// 高亮画笔
        /// </summary>
        public Brush HighlightForeground
        {
            get
            {
                return (Brush)GetValue(HighlightForegroundProperty);
            }
            set
            {
                SetValue(HighlightForegroundProperty, value);
            }
        }

        /// <summary>
        /// HighlightForegroundProperty 属性改变时,执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnHighlightForegroundChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as HighlightLabel;
            control.SetHighlight();
        }

        /// <summary>
        /// 要高亮的单词
        /// </summary>
        public static DependencyProperty HighlightWordProperty =
            DependencyProperty.Register("HighlightWord", typeof(string), typeof(HighlightLabel), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnHighlightWordChanged)));

        /// <summary>
        /// 要高亮的单词
        /// </summary>
        public string HighlightWord
        {
            get
            {
                return (string)GetValue(HighlightWordProperty);
            }
            set
            {
                SetValue(HighlightWordProperty, value);
            }
        }

        /// <summary>
        /// HighlightWordProperty 属性改变时,执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnHighlightWordChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as HighlightLabel;
            control.SetHighlight();
        }

        static HighlightLabel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HighlightLabel), new FrameworkPropertyMetadata(typeof(HighlightLabel)));
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            SetHighlight();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            SetHighlight();
        }

        /// <summary>
        /// 设置高亮
        /// </summary>
        private void SetHighlight()
        {
            var textBlock = (TextBlock)base.GetTemplateChild("PART_TextBlock");
            if (textBlock == null)
            {
                return;
            }

            var regex = new Regex(@"" + HighlightWord + @"", RegexOptions.IgnoreCase);
            var text = Content.ToString();
            var matches = regex.Matches(text);

            var index = 0;
            var runs = new List<Inline>();
            for (var i = 0; i < matches.Count; i++)
            {
                runs.Add(new Run
                {
                    Text = text.Substring(index, matches[i].Index - index)
                });
                index += matches[i].Index - index;

                runs.Add(new Run
                {
                    Text = matches[i].Value,
                    Foreground = HighlightForeground
                });
                index += matches[i].Length;
            }

            if (index != text.Length)
            {
                runs.Add(new Run
                {
                    Text = text.Substring(index)
                });
            }

            textBlock.Inlines.Clear();
            textBlock.Inlines.AddRange(runs);
        }
    }

Generic.xaml

    <Style TargetType="{x:Type customControls:HighlightLabel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type customControls:HighlightLabel}">
                    <TextBlock x:Name="PART_TextBlock" TextWrapping="Wrap"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

方法主要是,TextBlock可以通过多个 Run元素组合而成,通过正则表达式匹配到关键字,然后设置关键字的前景颜色,实现高亮的效果。

原文地址:https://www.cnblogs.com/fanful/p/14342488.html