locked
Change textblock height after data binding

    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

Answers

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

    <Page
        x:Class="App1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App1"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <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"/>
            </StackPanel>
        </Grid>
    </Page>
    

    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!!!

    Sunday, May 11, 2014 5:41 PM