Change textblock height after data binding RRS feed

  • Question

  • I have a datatemplate with a textblock with MaxLines set to 7 and a fixed width.

    When I set the text manually the textblock's height has automatically adapted to the the number of lines the text needs to fit in the textblock, with ofcourse a maximum of 7 lines.

    When I set te text by data binding, the height doesn't adapt.

    How can I force the textbox to adapt to the desired height after the data is bound?

    I need this to get a gridview with adapted item heights correspondending to the amount of text in the items.

    Sunday, May 11, 2014 5:13 PM


  • I just created a Sample and it works fine, see this:

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBox x:Name="input" Height="100" VerticalAlignment="Top" Grid.Column="0"/>
            <StackPanel Grid.Column="1" >
                <TextBlock x:Name="test" VerticalAlignment="Top" Text="{Binding ElementName=input, Path=Text}" MaxLines="7" TextWrapping="Wrap"/>
                <TextBox IsReadOnly="True" Text="This will start shifting when you add Text in 'input' Textbox"/>

    Since you are talking about datatemplate the you should be using this in a ListView or a GridView; so understand that all your items will need to fit in same size of block. If that is not what you want then try GridView with variable size items. 

    But overall you will not be able to achieve very dynamic item sizes.

    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    • Marked as answer by Anne Jing Thursday, May 22, 2014 6:28 AM
    Sunday, May 11, 2014 5:41 PM