xabush
xabush

Reputation: 877

wpf - TextBlock text not wrapping

I have a simple wpf window that has a TextBlock element with the following xaml code:

<TextBlock Grid.Row="3" Grid.Column="2" MaxWidth="370" 
x:Name="ResultTxt" Text="{Binding Text, Mode=OneWay}" TextWrapping="Wrap"></TextBlock>

When a new text is received from a server, I append the text to the TextBlock content. Here is the part of code that does that

Text = Text + e.Text; //e is an EventArgs
RaisePropertyChanged("Text");

When the text exceeds the current window limit, it doesn't wrap to a newline. Instead it just continues to the horizontal, although I have set the TextWrap property of the TextBlock to Wrap.

According to the suggestions in the answers to this question, the TextBlock should be inside a Grid container. But the TextBlock is in a Grid container and still not wrapping.

How can I make the TextBlock to wrap its contents into newline when it exceeds its window size?

Edit: Here is xaml code for the whole Grid container:

<Grid TextBlock.FontSize="16" DataContext="{Binding Main, Source={StaticResource Locator}}" HorizontalAlignment="Left" Width="760">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Capture Device" Margin="20,10,10,0"></TextBlock>
        <ComboBox ItemsSource="{Binding Path=CaptureDevices, Mode=OneWay}" DisplayMemberPath="ProductName" SelectedIndex="0" Margin="19,10,10,0" VerticalAlignment="Top" MinHeight="22" Grid.Row="0" Grid.Column="1" Name="DeviceCombo" SelectedItem="{Binding SelectedDevice, Mode=TwoWay}" IsEnabled="{Binding CanSelect}"/>
        <Slider Margin="19,10,10,0" Orientation="Horizontal" Value="{Binding MicrophoneLevel, Mode=TwoWay}" Maximum="100" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" IsEnabled="false" MaxWidth="150"/>
        <Button x:Name="StartBtn" Content="Start" Grid.Column="0" Grid.Row="1" Margin="30,10,5,25" Width="60" Command="{Binding StartRecodrding}" IsEnabled="{Binding CanStart}"/>
        <Button x:Name="StopBtn" Grid.Row="1" Content="Stop" Grid.Column="2" Margin="0,10,15,25" Width="60" Command="{Binding StopRecording}" IsEnabled="{Binding CanStop}"/>
        <ProgressBar x:Name="RecordLevel" Grid.Row="1" Grid.Column="3"  Margin="30,10,15,25" Minimum="0" Maximum="100" MinWidth="170" Value="{Binding CurrentLevel, Mode=OneWay}"></ProgressBar>
        <TextBlock x:Name="Status" Grid.Row="2" Grid.Column="2" Margin="15,15,10,10" Text="{Binding StatusText}"></TextBlock>
        <TextBlock Grid.Row="3" Grid.Column="2" MaxWidth="370" x:Name="ResultTxt" Text="{Binding Text, Mode=OneWay}" TextWrapping="Wrap"></TextBlock>
    </Grid>

Upvotes: 3

Views: 3986

Answers (1)

mm8
mm8

Reputation: 169390

The Width of the second column should not be set to Auto. Try to set it to *:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"></ColumnDefinition>
    <ColumnDefinition Width="Auto"></ColumnDefinition>
    <ColumnDefinition Width="*"></ColumnDefinition>
    <ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>

You should also avoid setting the Grid's Width to a fixed value. Try MaxWidth instead:

<Grid TextBlock.FontSize="16" DataContext="{Binding Main, Source={StaticResource Locator}}" HorizontalAlignment="Left" MaxWidth="760">

Upvotes: 1

Related Questions