开发者

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

I wish to customize the appea开发者_高级运维rance 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 in place of ticks along the tickbar. I want the position of the number to correspond to the value of a slider (much like the picture in the link).

I've searched around and one suggestion I found said to create a class that inherits from TickBar and override it's OnRender method.

I'd much rather find a solution that doesn't involve that. I was really hoping using a control template would do the trick. So, If there is one such solution, suggestions would be greatly appreciated! :)


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)); 
        }
    }
}


Here are 2 fast and easy solutions that do the job but should not be considered best practices:

A super fast fix would be to set the AutoToolTipPlacemant property on the slider to TopRight. This means numbers will show up in a tooltip when you drag the slider.

Option 2 is to edit a copy of the control template

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

and just create a few TextBlocks with your values in. This method is incredibly ramshackle and you should only use it if you want to use the special slider once. Also the numbers wont update if you change the Maximum and Minimum properties on the slider.

There is a proper way of solving this problem but if you need it done quickly and can't be bothered to start overriding OnRender or making a whole new control these ways will get the job done more quickly.

(the author of this answer post does in no way indorse sleazy coding practices or use these techniques as a substitute for the proper way of doing it in his software: they are just quick fixes) :-)


public class CustomTickBar:TickBar
{
    protected override void OnRender(DrawingContext dc)
    {
        Size size = new Size (base.ActualWidth,base.ActualHeight);

        int tickCount = (int)((this.Maximum - this.Minimum) / this.TickFrequency)+1;

        if ((this.Maximum - this.Minimum) % this.TickFrequency == 0)

            tickCount -= 1;

        Double tickFrequencySize;

        // Calculate tick's setting

        tickFrequencySize = (size.Width * this.TickFrequency / (this.Maximum - this.Minimum));

        string text = "";

        FormattedText formattedText = null;

        double num = this.Maximum - this.Minimum;

        int i = 0;

        // Draw each tick text

        for (i = 0;i <= tickCount;i++)
        {
            text = Convert.ToString (Convert.ToInt32 (this.Minimum + this.TickFrequency * i),10);

            //g.DrawString(text, font, brush, drawRect.Left + tickFrequencySize * i, drawRect.Top + drawRect.Height/2, stringFormat);

            formattedText = new FormattedText (text,CultureInfo.GetCultureInfo ("en-us"),FlowDirection.LeftToRight,new Typeface ("Verdana"),8,Brushes.Black);

            dc.DrawText (formattedText,new Point ((tickFrequencySize * i),30));
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜