locked
Perform Text Trimming on multi lines RRS feed

  • Question

  • I would like to perform text trimming on multi lines. I tried to use TextTrimming, but no ellipsis is shown. I found that when the line at the bottom edge is long enough, ellipsis can be shown.

    Therefore, I am trying to write a IValueConverter to add the ellipsis myself. I can use FormattedText to determine whether trimming is needed.However, I cannot find the suitable index to put the ellipsis. Is there any method to get the character index by providing the rectangle?

    Thanks.

    Franz

     

    My testing textblock :

     

    <TextBlock Text="{Binding Text}" TextTrimming="WordEllipsis" TextWrapping="WrapWithOverflow" Width="300" Height="50"/>
    

     

    My testing text is :

     

    model.Text = "Fly me to the moon\r\nLet me play among the stars\r\nLet me see what spring is like\r\nOn a-Jupiter and Mars\r\nIn other words, hold my hand\r\nIn other words, baby, kiss me\r\n";
    

     

    Wednesday, February 9, 2011 3:50 AM

Answers

  • I don't have any idea either. The main problem for this is "finding out the position to add ellipsis". I think I need to implement a wrapping algorithm to find out that position. However, I take a workaround. I can detect whether the text is too long by using FormattedText. A label with text "More..." is shown just under that TextBlock if the text is too long. And then I set the height of the TextBlock as a multiple of line height (so user will not see only the upper half of text). Franz
    • Proposed as answer by Sheldon _Xiao Friday, February 18, 2011 9:42 AM
    • Marked as answer by Franz Wong Tuesday, February 22, 2011 8:05 AM
    Thursday, February 17, 2011 10:48 AM

All replies

  • hi Franz Wong,

    first of all nice lyrics.

    secondly, i don't think what you trying to do is achievable via the out of the box TextBlock.

    as the trimming it self is designed to kick in when the content as a whole stretches beyond the pre-defined height & width attributes. so a work around is to manually append your ellipsis within each line just before the line breakers.

    e.g.:

    model.Text = "Fly me to the moon...\r\nLet me play among the stars...\r\nLet me see what spring is like...\r\nOn a-Jupiter and Mars...\r\nIn other words, hold my hand...\r\nIn other words, baby, kiss me...\r\n";

    or you can write a function to search your string and insert the ellipsis automatically at your line breaks.


    hope this is of any help.

    Wednesday, February 9, 2011 4:03 AM
  • Thanks for reply. :-)

    I just want to put to the ellipsis to the "last displayable line" instead of the lines before it. I know the problem is how to find the "last displayable line".

    If other solutions exist, I can use other controls instead of TextBlock.

    Franz

    Wednesday, February 9, 2011 6:26 AM
  • Hi Franz Wong,

    Based on your description, I think what you need is "TextWrapping="Wrap"", instead of "TextWrapping="WrapWithOverflow"".

    Am I right?

    If I am wrong, could you share your main concern with me?

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 11, 2011 2:14 AM
  • Sheldon : WrapWithOverflow does not help.

    My case is when the last displayable line is SHORT, the ellipsis is not shown.

     

    For example, when the TextBlock can only show 3 lines and it can only shows 5 characters on each line.

    Case 1, the text is "12345678\r\n12345678\r\n12345678\r\n12345678\r\n"

     

    The list box shows

    123...

    123...

    123...

     

    Case 2, the text is "123\r\n123\r\n123\r\n123\r\n"

    The list box shows

    123

    123

    123

    Actually there is still ONE more line in case 2, ellipsis should be shown. However, it is not.

    Saturday, February 12, 2011 6:55 AM
  • Hi Franz Wong,

    Thank you for your feedback.

    I find a way you could have a try, refer to this thread:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a845f31e-577c-4829-8166-75277c047955/#

    DutchMarcel has provided a good idea, you could adapt his reply to your sepcial needs.

     Because I am not sure the special needs and your situation, I think it is hard to come up a perfect solution,

    And if your issue persists, could you share a simple sample with me to reproduce your issue, and tell me your main purpose, I think it will be beneficial for me to resolve your issue or provide you a workaround.

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, February 12, 2011 7:37 AM
  • Thanks for your reply.

    I have read that thread and that is not what I want. I don't want to concatenate the lines into a single line.

    I have created a simple sample. The sample should only shows 3 lines and I EXPECT the end of the third line should contain ellipsis.

    Franz

     

    XAML :

    <Window x:Class="TextTrimingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
      <Grid>
    		<TextBlock Name="textBlock" TextTrimming="WordEllipsis" TextWrapping="Wrap" Width="300" Height="50" Background="SkyBlue" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    	</Grid>
    </Window>
    

    Code Behind :

    namespace TextTrimingTest {
    	/// <summary>
    	/// Interaction logic for MainWindow.xaml
    	/// </summary>
    	public partial class MainWindow : Window {
    		public MainWindow() {
    			InitializeComponent();
    			textBlock.Text = "Fly me to the moon\r\nLet me play among the stars\r\nLet me see what spring is like\r\nOn a-Jupiter and Mars\r\nIn other words, hold my hand\r\nIn other words, baby, kiss me\r\n";
    		}
    	}
    }
    

    Saturday, February 12, 2011 8:13 AM
  • Hi Franz Wong,

    I think I have understand your question clearly, however, i have not come up a solution basen on your purpose.

    Do you have new findings or updates?

    I think we could work for it together.

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 17, 2011 9:50 AM
  • I don't have any idea either. The main problem for this is "finding out the position to add ellipsis". I think I need to implement a wrapping algorithm to find out that position. However, I take a workaround. I can detect whether the text is too long by using FormattedText. A label with text "More..." is shown just under that TextBlock if the text is too long. And then I set the height of the TextBlock as a multiple of line height (so user will not see only the upper half of text). Franz
    • Proposed as answer by Sheldon _Xiao Friday, February 18, 2011 9:42 AM
    • Marked as answer by Franz Wong Tuesday, February 22, 2011 8:05 AM
    Thursday, February 17, 2011 10:48 AM
  • Hi Franz Wong,

    Could you recept this workaround, and could you close this thread as "Answerred"?

     

    Best regards,


    Sheldon _Xiao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 22, 2011 7:58 AM
  • I have marked it. However, I think Microsoft should also think about the behavior of Text wrapping for this situation.
    Tuesday, February 22, 2011 8:05 AM
  • Hello,

    How you fight with this situation?

    Thanks.

    Tuesday, January 7, 2014 9:43 AM