using System.Windows; using System.Windows.Controls; namespace Muchinfo.WPF.Controls.Zoom { /// /// Class ZoomFontText. /// public class ZoomFontText : Control { static ZoomFontText() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomFontText), new FrameworkPropertyMetadata(typeof(ZoomFontText))); } public double FontCount { get { return (double)GetValue(FontCountProperty); } set { SetValue(FontCountProperty, value); } } // Using a DependencyProperty as the backing store for FontCount. This enables animation, styling, binding, etc... public static readonly DependencyProperty FontCountProperty = DependencyProperty.Register("FontCount", typeof(double), typeof(ZoomFontText), new PropertyMetadata(2d)); /// /// 放大倍数 /// public double ZoomFontSize { get { return (double)GetValue(ZoomFontSizeProperty); } set { SetValue(ZoomFontSizeProperty, value); } } // Using a DependencyProperty as the backing store for ZoomSize. This enables animation, styling, binding, etc... public static readonly DependencyProperty ZoomFontSizeProperty = DependencyProperty.Register("ZoomFontSize", typeof(double), typeof(ZoomFontText), new PropertyMetadata(2d)); public string NormalText { get { return (string)GetValue(NormalTextProperty); } set { SetValue(NormalTextProperty, value); } } // Using a DependencyProperty as the backing store for NormalText. This enables animation, styling, binding, etc... public static readonly DependencyProperty NormalTextProperty = DependencyProperty.Register("NormalText", typeof(string), typeof(ZoomFontText), new PropertyMetadata("")); /// /// 放大文本 /// public string ZoomText { get { return (string)GetValue(ZoomTextProperty); } set { SetValue(ZoomTextProperty, value); } } // Using a DependencyProperty as the backing store for ZoomText. This enables animation, styling, binding, etc... public static readonly DependencyProperty ZoomTextProperty = DependencyProperty.Register("ZoomText", typeof(string), typeof(ZoomFontText), new PropertyMetadata("")); public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ZoomFontText), new PropertyMetadata("", OnTextChange)); private static void OnTextChange(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = d as ZoomFontText; if (self == null) return; // SetViewBoxWidthHeight(self); var text = e.NewValue as string; if (string.IsNullOrWhiteSpace(text)) return; if (text.Length < self.FontCount) { self.NormalText = text; self.ZoomText = string.Empty; return; } self.NormalText = text.Substring(0, text.Length - (int)self.FontCount); self.ZoomText = text.Substring(text.Length - (int)self.FontCount); } } }