none
TextBox doesn't resize control

    Question

  • Hi. When I write text in TextBox control and TextWrapping = True - TextBox doesn't resize height.

    View

    <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
    
            <TextBox MinHeight="27"
                     TextWrapping="Wrap"
                     Width="50" />
        </Grid>


    Text to reproduce: "test tes" (without "") on this character "s", text is second line and TextBox control not resize height. Bug?

    Edit:

    Link to project http://www74.zippyshare.com/v/qlypxRlc/file.html


    • Edited by MichaelWPL Tuesday, December 22, 2015 10:57 AM
    Tuesday, December 22, 2015 10:42 AM

All replies

  • When I put that markup in an empty mainpage, it works fine for me.

    I can't reproduce your issue.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by MichaelWPL Tuesday, December 22, 2015 3:16 PM
    • Unmarked as answer by MichaelWPL Tuesday, December 22, 2015 3:16 PM
    Tuesday, December 22, 2015 2:03 PM
  • When I put that markup in an empty mainpage, it works fine for me.

    I can't reproduce your issue.

    You used the project from link http://www74.zippyshare.com/v/qlypxRlc/file.html

    ?

    • Edited by MichaelWPL Tuesday, December 22, 2015 3:18 PM
    Tuesday, December 22, 2015 3:17 PM
  • No, I didn't.

    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 22, 2015 4:29 PM
  • Please try project from link.

    In the last version of Visual Studio 2015 Community is reproduce.

    Silverlight project is OutOfBrowser.

    Project from VS 2015 Community http://www53.zippyshare.com/v/6AAglCs0/file.html


    • Edited by MichaelWPL Tuesday, December 22, 2015 5:19 PM
    Tuesday, December 22, 2015 4:41 PM
  • OK, I managed to get it to fail.

    You can greatly reduce the likelihood of the problem by putting a padding on the textbox.

            <TextBox 
               Padding="2,3,5,3"
                     MinHeight="27"
                     TextWrapping="Wrap" 
                     Width="50"  />

    The space seems to mess the measuring up.

    It does look like a bug to me but with Silverlight in end of lifecycle I think you'll be lucky if you could get anyone allocated to work on it.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 22, 2015 6:12 PM
  • Unfortunately padding is not good idea.

    Now text to reproduce is: ss ssss

    In my opinion this is a silverlight bug, because in WPF works correctly.


    • Edited by MichaelWPL Tuesday, December 22, 2015 6:22 PM
    Tuesday, December 22, 2015 6:20 PM
  • Whilst you're holding your lucky rabbit's paw, 4 leaf clover and any other luck attracting objects.

    Waiting to see if the bug is fixed, you could try this work round.

    The problem seems to be in measuring space.

    Handle textchanged and do:

            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                TextBox tb = (TextBox)sender;
                tb.Text.Replace(' ', '_');
                tb.InvalidateMeasure();
                tb.Text.Replace('_', ' ');
            }

    And hope they don't use underscores.

    If they do then you could pick some other wide character.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 22, 2015 7:28 PM
  • This solution not repair the problem.

    Bug still exists.

    Tuesday, December 22, 2015 7:42 PM
  • No, it's a work round.

    Good luck.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Tuesday, December 22, 2015 8:37 PM
  • //tb.Text == "test tes"

    tb.Text.Replace(' ', '_'); <- this not replace text

    //tb.Text == "test tes"

    string replacedText = tb.Text.Replace(' ', '_'); <- in replacedText is new text

    //replacedText == "test_tes", tb.Text == "test tes"


    I try:
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                var textBox = (TextBox)sender;
    
                textBox.TextChanged -= TextBox_TextChanged;
    
                textBox.Text = textBox.Text.Replace(" ", "_");
                textBox.InvalidateMeasure();
                textBox.Text = textBox.Text.Replace("_", " ");
    
                textBox.TextChanged += TextBox_TextChanged;
            }

    Unfortunately this solution not repair the problem with code from first message and text "test tes" :(
    • Edited by MichaelWPL Tuesday, December 22, 2015 11:43 PM
    Tuesday, December 22, 2015 11:41 PM
  • It seems the problem is with the auto sizing of the container it's in. For whatever reason, the textbox doesn't persuade it to grow as it should.

    That means you could probably coerce something of force measure arrange on the parent.

    Or.

    You could use this:

            <Grid>
                <TextBlock Text="{Binding Text, ElementName=tb, Mode=TwoWay}"
                   TextWrapping="Wrap" Width="{Binding Width, ElementName=tb, Mode=TwoWay}"
                   Padding="5"/>
                <TextBox Name="tb"
                     MinHeight="27"
                     TextWrapping="Wrap" 
                     Width="50"  >
                </TextBox>
            </Grid>

    What seems to happen is the textblock demands the extra space and the container grows even when the textbox hasn't.

    Like I say, there's possibly a more elegant solution.

    You could build a controltemplate which incorporates a textblock in a textbox like some watermark textbox solutions do.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Wednesday, December 23, 2015 3:30 PM
  • This works... but text "test tet" create two lines and text located is in one line.

    It is better. It is better.

    However, can I wait for microsoft to fix this bug?

    This is bug or not?

    Wednesday, December 23, 2015 4:57 PM
  • Hi MichaelWPL,

    Thanks for Andy's reply.

    I also reproduce your problem and the workaround Andy provided can resolved this problem.

    >>However, can I wait for microsoft to fix this bug? This is bug or not?

    I suggest you post this problem to Visual Studio Connect to feedback this problem from below link. Microsoft engineer will response for this problem when they see your feedback.

    https://connect.microsoft.com/VisualStudio/Feedback/LoadSubmitFeedbackForm?FormID=6270

    Best Regards,
    Weiwei

    Thursday, December 24, 2015 3:19 AM
    Moderator
  • You could alternatively use a richtextbox.

    This seems to calculate height fine when I try.

            <RichTextBox  MinHeight="27"
                     TextWrapping="Wrap"
                     Width="50" />

    There's one teeny tiny problem with the rtb.
    Straight out the box, you can't bind Text.

    OK, you're thinking that's a HUGE problem and wtf is that idiot on about.

    You can inherit and extend, add Text as a dependency property:

    https://marlongrech.wordpress.com/2009/03/25/richtextbox-text-property-where-are-you-hiding/

    The rtb comes with a shed load of functionality you may not want, so there are some downsides mean it isn't a total no brainer choice.

    I suppose you could use a rtb rather than a textblock and that would obviate the need for padding which can give you that extra space.

    .

    Not that I'm saying this isn't a problem or anything.. but.

    I've seen users quite happily using applications were throwing up spurious errors regularly. When asked they say something like "Oh, it just does that. What? You think that's a problem."

    .

    Happy Christmas !


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Thursday, December 24, 2015 2:32 PM
  • My workaround: Derive from TextBox Class, use MeasureOverride, and reduce available width a little bit:

    public class TextBoxEx : TextBox
        {
            protected override Size MeasureOverride(Size availableSize)
            {
                var size = base.MeasureOverride(new Size(Math.Max(0, availableSize.Width - 1), availableSize.Height));
                return size;
            }
        }

    Then use TextBoxEx instead of TextBox everywhere in your project

    Tuesday, September 11, 2018 8:12 AM