WPF中ProgressBar上的文本 [英] Text on a ProgressBar in WPF

查看:243
本文介绍了WPF中ProgressBar上的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能对WPF知识不言而喻,但是我想知道是否有一种简单的方法可以在WPF ProgressBar上放置文本。对我来说,一个空的进度条看起来很裸。该屏幕空间可能会显示有关正在进行中的消息,甚至可以在表示中添加数字。现在,WPF就是关于容器和扩展的,我正在慢慢地解决这个问题,但是由于我没有看到 Text或 Content属性,因此我想我必须添加一些内容到我进度条所在的容器中有没有比我最初的WinForms冲动更自然的一种或两种技术?将文本添加到进度条的最佳,最WPF方式是什么?

This may be a no-brainer for the WPF cognoscenti, but I'd like to know if there's a simple way to put text on the WPF ProgressBar. To me, an empty progress bar looks naked. That's screen real estate that could carry a message about what is in progress, or even just add numbers to the representation. Now, WPF is all about containers and extensions and I'm slowly wrapping my mind around that, but since I don't see a "Text" or "Content" property, I'm thinking I'm going to have to add something to the container that is my progress bar. Is there a technique or two out there that is more natural than my original WinForms impulses will be? What's the best, most WPF-natural way to add text to that progress bar?

推荐答案

如果您需要一个可重用的添加文本的方法,您可以创建一个新的Style / ControlTemplate,它具有一个额外的TextBlock来显示文本。您可以劫持TextSearch.Text附加属性以在进度栏上设置文本。

If you are needing to have a reusable method for adding text, you can create a new Style/ControlTemplate that has an additional TextBlock to display the text. You can hijack the TextSearch.Text attached property to set the text on a progress bar.

如果不需要可重复使用,只需将进度条放在网格中,然后将TextBlock添加到网格中。由于WPF可以将元素组合在一起,因此可以很好地工作。

If it doesn't need to be reusable, simply put the progress bar in a Grid and add a TextBlock to the grid. Since WPF can compose elements together, this will work nicely.

如果需要,可以创建一个UserControl,将ProgressBar和TextBlock公开为公共属性,这样就可以了。比创建自定义ControlTemplate少的工作。

If you want, you can create a UserControl that exposes the ProgressBar and TextBlock as public properties, so it would be less work than creating a custom ControlTemplate.

这篇关于WPF中ProgressBar上的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆