如何使用字符串值代替WPF刻度栏上的刻度? [英] How to use string values in place of ticks on WPF Tickbar?
问题描述
我希望自定义基本WPF的外观 TickBar
。我想知道是否有使用控制模板执行此操作的简单方法:
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 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).
我到处搜索,发现一个建议说创建一个从 TickBar
继承的类并覆盖它 OnRender
方法。
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. :-)
重写 OnRender
似乎是最明显的解决方案。我真的很希望使用各种模板... 叹气 ...很好。无论如何,我遇到了此讨论在MSDN的论坛上提供了答案,以正确的方向向我发送邮件。
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));
}
}
}
这篇关于如何使用字符串值代替WPF刻度栏上的刻度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!