调整标签大小

发布于 2024-07-19 16:54:02 字数 425 浏览 3 评论 0原文

我有一个 WPF 图表,其中有很多标签。 这些标签上的文本是动态加载的并且可能会发生变化。 如果我将宽度设置为自动,那么这些标签可能会重叠,这使得文本不可读。

该图表支持多种尺寸,因此如果它变大,则条形图会重新调整大小,并且有更多的文本空间。 现在我想将文本调整到可用的空间。 如果它变得太小,我不想再显示标签(工具提示可用,因此用户仍然可以获得所需的信息)。 考虑字符串“Case 1, blah blah”,可能没有足够的空间来显示整个字符串,而只能显示第一个单词。 在本例中,我希望字符串为“Case 1..”,其中 .. 表示工具提示中有更多信息。

我可以确定字符串的可用长度。 但是我如何确定单个字母所占用的空间呢? 当然,我也可以只调整标签的大小,但它会在任何地方切断字符串,这可能对用户没有帮助(而且看起来很丑)。

有任何想法吗?

I have a chart in WPF with a lot of labels. The text on these labels is dynamically loaded and subject to change. If I set the width just to auto, then these labels may overlap, which makes the text unreadable.

The chart support multiple sizes, so if it gets larger, then the bars are re sized and there is more space for text. Now I want to adjust the text to the space which is available. If it gets too small, I don't want to display the label anymore (a tooltip is available, so the user still gets the required information). Consider the string "Case 1, blah blah", there is probably not enough space to display the whole string, but just the first word. In this case I want the string to be "Case 1..", with .. indicating that there is some more information in the tooltip.

I can determine the length available for the string. But how can I determine the space a single letter will take? Of course I could also just re size the label, but then it would just cut off the string anywhere which is probably not helpful for the user (and looks ugly).

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

番薯 2024-07-26 16:54:02

如果您可以使用 TextBlocks 而不是标签,那么它们有一个 TextTrimming< /a> 属性将为您执行此操作到最近的字符或最近的单词。


虽然您似乎对 TextTrimming 属性感到满意,但我将对其进行编辑以添加 TextBox 控件具有 GetRectFromCharacterIndex 方法,只要字体设置与您的标签相匹配,您就可以找出一个或多个字符在屏幕上的大小。 如果您想在标签中的特定位置而不是最近的字符/单词进行修剪,这可能很有用。

If you can use TextBlocks instead of labels then they have a TextTrimming property which will do this for you to either the nearest character or the nearest word.


While you seem happy with the TextTrimming property, I'll edit this to add that the TextBox control has a GetRectFromCharacterIndex method that would allow you to find out the size on screen of one or more characters as long as the font settings matched your label. This might be useful if you wanted to trim at specific places in the label rather than the nearest character / word.

杯别 2024-07-26 16:54:02

我不是 WPF 专家,但我认为您需要使用代码而不是 XAML 来完成此操作。

首先获取文本可用空间的实际像素宽度。
然后查看 XAML 前端使用的字符集、点距等,并从中计算每个字符所需的像素宽度。

您还可以考虑更改字符大小以及减少标签长度。

Not an expert in WPF, but I would think that you'll need to do this in code rather than XAML.

Start by obtaining the actual pixel width of the space available for the text.
Then look at the character set, dot pitch etc. utilised on the XAML front end and from there calculate the pixel width required per character.

You could also look at changing the character sizes as well as reducing the label length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文