using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Threading; namespace FWindSoft.Wpf.Controls { /// /// 搜索使用textBlock /// public class SearchTextBlock:TextBlock { public static readonly DependencyProperty SearchBackgroundProperty = DependencyProperty.Register("SearchBackground", typeof(Brush), typeof(SearchTextBlock), new PropertyMetadata(Brushes.Yellow, new PropertyChangedCallback(PropertyChangedCallback))); public static readonly DependencyProperty SearchForegroundProperty = DependencyProperty.Register("SearchForeground", typeof(Brush), typeof(SearchTextBlock), new PropertyMetadata(Brushes.Black, new PropertyChangedCallback(PropertyChangedCallback))); #region 关键字附加属性 public static readonly DependencyProperty SearchKeyProperty = DependencyProperty.RegisterAttached("SearchKey", typeof(string), typeof(SearchTextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits ,new PropertyChangedCallback(PropertyChangedCallback))); public static string GetSearchKey(DependencyObject dp) { return dp.GetValue(SearchKeyProperty) as string; } public static void SetSearchKey(DependencyObject dp, string value) { dp.SetValue(SearchKeyProperty, value); } #endregion static SearchTextBlock() { try { TextProperty.OverrideMetadata(typeof(SearchTextBlock), new FrameworkPropertyMetadata((object)string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(PropertyChangedCallback))); } catch (Exception e) { } } public Brush SearchBackground { set { SetValue(SearchBackgroundProperty, value); } get { return (Brush)GetValue(SearchBackgroundProperty); } } public Brush SearchForeground { set { SetValue(SearchForegroundProperty, value); } get { return (Brush)GetValue(SearchForegroundProperty); } } public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { if (!(d is SearchTextBlock )) { return; } var block = (SearchTextBlock) d; block.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(block.RefreshShow)); } catch (Exception ex) { } } #region 内部编辑 //内部编辑模块和BeginInvoke(DispatcherPriority.Background基本可替代?,有时间再验证 #endregion public void RefreshShow() { try { var stringKey = GetSearchKey(this); if (string.IsNullOrEmpty(stringKey)) return; string newStr = this.Text ?? string.Empty; this.Inlines.Clear(); int index = -1; while ((index = newStr.IndexOf(stringKey)) > -1) { if (index > 0) { this.Inlines.Add(new Run(newStr.Substring(0, index))); } this.Inlines.Add(new Run(newStr.Substring(index, stringKey.Length)) { Background = this.SearchBackground, Foreground = this.SearchForeground }); newStr = newStr.Substring(index + stringKey.Length); } this.Inlines.Add(new Run(newStr)); } catch (Exception ex) { Debug.Write(ex.Message); } } } }