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) ; if (i == this.Maximum) { x = x - 5;//减去5,防止最后数字不显示 } dc.DrawText(formattedText, new Point(x, 10)); } } }
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)); } } }
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); double x = tickFrequencySize * i; double y = 5; if (i == this.Maximum) { x = x - 5; } Pen pen = new Pen(Brushes.Red,2); dc.DrawLine(pen, new Point(x+2, 0), new Point(x+2,y)); dc.DrawText(formattedText, new Point(x, y)); } } }