locked
How the best way to do a "Read More" link? RRS feed

  • Question

  • How the best way to do a "Read More" link?

    I need to truncate a text and put the link "read more" using silverlight 4 and Visual Studio 2010!

    I was tryed with textBlock but doesn't work!

    Wednesday, December 29, 2010 11:46 AM

Answers

  • I managed to solve the problem using the following solution:

    * Placed in  xaml page a "WrapPanel.
    * And put on the Cs page the following code:

    private void wp01_Loaded(object sender, RoutedEventArgs e)
            {
                int sizeText = 500;
                WrapPanel wp = sender as WrapPanel;
                string textComment = wp.DataContext.ToString();
                if (textComment.Length > sizeText)
                {
                    textComment = textComment.Substring(0, sizeText - 12) + "...";
                    string[] myTextWords = textComment.Split(new Char[] { ' ' });
                    for (int j = 0; j < myTextWords.Length; j++)
                    {
                        TextBlock txt = new TextBlock();
                        txt.Text = myTextWords[ j ].ToString() + " ";
                        wp.Children.Add(txt);
                    }
                    HyperlinkButton link = new HyperlinkButton();
                    link.Content = "Read More";
                    link.Click += new RoutedEventHandler(link_Click);
                    wp.Children.Add(link);
                    wp.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                }
                else
                {
                    TextBlock txt = new TextBlock();
                    txt.TextWrapping = TextWrapping.Wrap;
                    txt.Text = textComment;
                    wp01.Children.Add(txt);
                }            
           }
     }


     

    Still missing implement a window in which to display the full text without the hyperlink. But this solution is ok!

    Wednesday, January 5, 2011 12:18 PM

All replies

  • you can have a textblock and link button side by side. Use stack panel or grid to order it. When the user click on the link button pass the Id (of row to fetch the whole text) to popup window.

    Thursday, December 30, 2010 7:41 AM
  • ChildWindow that's good ideia, but I don't know how Trim the text as if it is too long and add a hyperlink for display all text in a ChildWindow for example.

    Friday, December 31, 2010 7:04 AM
  • If you don't know how to trim text, you in deep problem

    Friday, December 31, 2010 9:15 AM
  • Trim is not the problem!

    The problem is to set the text dynamically according to the size of the TextBlock. TextBlock should adjust the size of the page!

    If you have suggestions I'd be thankfull.

    Friday, December 31, 2010 12:35 PM
  • I managed to solve the problem using the following solution:

    * Placed in  xaml page a "WrapPanel.
    * And put on the Cs page the following code:

    private void wp01_Loaded(object sender, RoutedEventArgs e)
            {
                int sizeText = 500;
                WrapPanel wp = sender as WrapPanel;
                string textComment = wp.DataContext.ToString();
                if (textComment.Length > sizeText)
                {
                    textComment = textComment.Substring(0, sizeText - 12) + "...";
                    string[] myTextWords = textComment.Split(new Char[] { ' ' });
                    for (int j = 0; j < myTextWords.Length; j++)
                    {
                        TextBlock txt = new TextBlock();
                        txt.Text = myTextWords[ j ].ToString() + " ";
                        wp.Children.Add(txt);
                    }
                    HyperlinkButton link = new HyperlinkButton();
                    link.Content = "Read More";
                    link.Click += new RoutedEventHandler(link_Click);
                    wp.Children.Add(link);
                    wp.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                }
                else
                {
                    TextBlock txt = new TextBlock();
                    txt.TextWrapping = TextWrapping.Wrap;
                    txt.Text = textComment;
                    wp01.Children.Add(txt);
                }            
           }
     }


     

    Still missing implement a window in which to display the full text without the hyperlink. But this solution is ok!

    Wednesday, January 5, 2011 12:18 PM