wpf - TextBlock文本不包装

时间:2017-03-31 11:40:16

标签: c# wpf xaml

我有一个简单的wpf窗口,其中包含 TextBlock 元素,其中包含以下 xaml 代码:

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

当从服务器收到新文本时,我将文本附加到TextBlock内容。以下是执行该操作的代码部分

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

当文本超出当前窗口限制时,它不会换行到换行符。相反,它只是继续到水平,虽然我已将TextBlock的 TextWrap 属性设置为Wrap。

根据this question答案中的建议,TextBlock应该位于Grid容器中。但TextBlock在Grid容器中,仍然没有包装。

当超出窗口大小时,如何让TextBlock将其内容包装成换行符?

编辑:以下是整个网格容器的xaml代码:

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

1 个答案:

答案 0 :(得分:0)

第二列的Width不应设置为Auto。尝试将其设置为*

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

您还应该避免将Grid&#39; Width设置为固定值。请尝试MaxWidth

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