WPF TextBlock 每个文字一个颜色

啥也不多说 附上源码


public class MyTextBlock:TextBlock
    {
        public static readonly DependencyProperty NewForeColorProperty = DependencyPropertyManager.Register("NewForeColor",
            typeof(Brush), typeof(MyTextBlock), new PropertyMetadata(null));

        public Brush NewForeColor
        {
            get { return (Brush)GetValue(NewForeColorProperty); }
            set { SetValue(NewForeColorProperty, value); }
        }

        public static readonly DependencyProperty NewTextProperty = DependencyPropertyManager.Register("NewText",
            typeof(string), typeof(MyTextBlock), new PropertyMetadata(""));

        public string NewText
        {
            get { return (string)GetValue(NewTextProperty); }
            set { SetValue(NewTextProperty, value); }
        }

        public override void EndInit()
        {
            for(int i = 0; i < this.NewText.Length; i++)
            {
                Run run = new Run(this.NewText.Substring(i,1));
                run.Foreground = this.NewForeColor;
                this.Inlines.Add(run);
            }
        }
    }

你可能感兴趣的:(.NET)