问题描述
我希望自定义基本 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. :-)
Override OnRender
似乎是最明显的解决方案.我真的很希望使用某种模板...sigh ...嗯.无论如何,我遇到了 这个讨论 在 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 Tickbar 上的刻度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!