using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Media; namespace WpfAppTest { internal class RebarTextBlock : TextBlock { public static readonly DependencyProperty RebarTextProperty = DependencyProperty.Register( nameof(RebarText), typeof(string), typeof(WpfAppTest.RebarTextBlock), new PropertyMetadata(string.Empty, OnRebarTextChanged)); public string RebarText { get => (string)GetValue(RebarTextProperty); set => SetValue(RebarTextProperty, value); } private static void OnRebarTextChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { ((WpfAppTest.RebarTextBlock)d).BuildInlines(e.NewValue as string); } //{Assembly.GetExecutingAssembly().GetName()} internal static readonly FontFamily RebarFont = new($"pack://application:,,,/WpfAppTest;component/Fonts/#SJQY"); internal static readonly FontFamily NormalFont = new($"pack://application:,,,/WpfAppTest;component/Fonts/#汉仪粗宋简"); private void BuildInlines(string text) { var sjqy = (FontFamily)Application.Current.Resources["SJQY"]; Inlines.Clear(); if (string.IsNullOrEmpty(text)) return; var regex = new Regex( @"(? lastIndex) { this.Inlines.Add(new Run { Text = text.Substring(lastIndex, match.Index - lastIndex), FontFamily = NormalFont }); } // 钢筋等级(SJQY) this.Inlines.Add(new Run { Text = match.Value, FontFamily = sjqy }); lastIndex = match.Index + match.Length; } // 尾部普通文本 if (lastIndex < text.Length) { this.Inlines.Add(new Run { Text = text.Substring(lastIndex), }); } } } }