locked
Find the width of a text string inside a text box?

    Question

  • Hi,

    I have a text box with a long text string in it. I would like to know what the width of the text string is so that I can scroll it inside the textbox using the text box scrollviewer.

                var sv = this.foodieItemText.GetFirstDescendantOfType<ScrollViewer>();
                sv.ScrollToHorizontalOffsetWithAnimation(this.foodieItemText.Width, 20);
    

    The scrolling works fine but I don't know what value to use for the width offset in the first argument to ScrollToHorizontalOffsetWithAnimation. For my purposes I want it to scroll the full length of the text string (which is longer than the actual with of the text box). Any ideas?

    Monday, January 26, 2015 8:10 PM

Answers

  • Hi duffybr,

    Yes, there is a ScrollViewer inside TextBox template and we could scroll to the specific position as we need, however there is no direct way to get the string width in TextBox, however we could get the width in TextBlock, by measure method.

    See this discussion for more information: http://stackoverflow.com/questions/9126076/how-can-i-calculate-the-width-of-a-string-in-metro-without-displaying-it

    Besides, there are some third party libraries help us get the String width.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by duffybr Friday, January 30, 2015 2:14 PM
    Tuesday, January 27, 2015 7:35 AM
    Moderator