How to use string values in place of ticks on WPF Tickbar?

前端 未结 3 1360
有刺的猬
有刺的猬 2021-01-04 12:32

I wish to customize the appearance of the basic WPF TickBar. I was wondering if there was a simple way to do this using a control template:

I wish to have numbers i

3条回答
  •  耶瑟儿~
    2021-01-04 12:57

    Okay, I have a solution. I figured I should answer my question in the event some other fellow along the line runs into the same situation as I. :-)

    Override OnRender seems to be the most obvious solution. I was really hoping to use a template of sorts...sigh ...ah well. Anyways, I ran across this discussion on MSDN's forums which provided an answer to send me in the right direction.

    Simple, and it need a few tweeks to make it more flexible, so here's my version:

    class CustomTickBar : TickBar
    {
        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            double num = this.Maximum - this.Minimum;
            double y = this.ReservedSpace * 0.5;
            FormattedText formattedText = null;
            double x = 0;
            for(double i = 0; i <= num; i += this.TickFrequency)
            {
                formattedText = new FormattedText(i.ToString(), FlowDirection.LeftToRight, 
                    new Typeface("Verdana"), 8, Brushes.Black);
                if(this.Minimum == i)
                    x = 0;
                else
                    x += this.ActualWidth / (num / this.TickFrequency) ;
                dc.DrawText(formattedText, new Point(x, 10)); 
            }
        }
    }
    

提交回复
热议问题