WPF 关键字高亮实现方式

 Label自定义控件

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(@"\b" + HighlightWord + @"\b", 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 local:HighlightLabel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:HighlightLabel}">
                    <TextBlock x:Name="PART_TextBlock" TextWrapping="Wrap"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

在前端xaml中使用方式:

<local:HighlightLabel x:Name="PART_TextBlock" HighlightWord="{Binding HighLightWord}"/>
                                

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值