Resizing labels
I have a diagram in WPF with a lot of shortcuts. The text on these labels is dynamically loaded and can be changed. If I only set the width to auto, these labels may overlap, making the text unreadable.
The chart supports multiple sizes, so if it gets bigger then the bars are sized and there is more room for text. Now I want to adjust the text to the available space. If it gets too small, I no longer want to display the shortcut (a tooltip is available, so the user still gets the information they want). Consider the line "Case 1 blah blah", there is probably not enough room to display the entire line, but only the first word. In this case, I want the string to be "Case 1 ..", indicating that there is more information in the tooltip.
I can determine the length available for the string. But how can I determine the space that one letter will take? Of course, I could just resize the label, but then it just cut the string anywhere, which is probably not useful to the user (and looks ugly).
Any ideas?
a source to share
If you can use TextBlocks instead of labels, then they have a TextTrimming property that will do that for you either to the nearest character or the nearest word.
While you're happy with the TextTrimming property, I'll edit that to add that the TextBox control has a GetRectFromCharacterIndex method that will let you know the size of one or more characters on screen if the font options match your label. This can be useful if you want to crop at specific locations on the label, rather than the nearest character / word.
a source to share
Not an expert in WPF, but I think you will need to do this in code, not XAML.
Start by getting the actual pixel width of the space available for the text. Then look at the character set, dot spacing, and so on used at the front end of the XAML, and from there calculate the pixel width required for each character.
You can also look at resizing the symbols as well as reducing the length of the label.
a source to share