如何在textBlock工作中获得换行符

时间:2015-04-14 08:25:26

标签: c# wpf xaml textblock

我似乎无法在我的TextBlock中获得换行符。

TextWrapping="Wrap"无效。此外,TextTrimming等任何其他内容根本不会影响我的TextBlock。我假设另一个控件是阻塞的?谈论" txtErrorLabel"

<Window x:Class="BLVKServiceDashboard.Pages.ErrorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ErrorWindow" Height="555" Width="420" Icon="/BLVKServiceDashboard;component/error.ico" ResizeMode="NoResize" WindowStartupLocation="Manual" ShowInTaskbar="False" Background="#FFEEEEEE" WindowStyle="None">
<Grid Margin="0,0,4,-3" RenderTransformOrigin="0.588,0.554">
    <Label Content="Fehlermeldungen" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontFamily="Segoe UI Semibold" FontSize="25"/>

    <ListBox x:Name="lstErrorItems" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderThickness="0" Background="{x:Null}" Margin="0,58" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="500" Height="38" Margin="0,0,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="380"/>
                        <ColumnDefinition Width="120"/>
                    </Grid.ColumnDefinitions>
                    <ScrollViewer Grid.ColumnSpan="2">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Foreground="Black" Text="Fehler: " HorizontalAlignment="Left" Height="Auto" Width="Auto" VerticalAlignment="Top"  Margin="5,10,0,2" FontSize="14" FontFamily="Segoe UI" ScrollViewer.VerticalScrollBarVisibility="Disabled"/>
                            <TextBlock x:Name="txtErrorLabel" Foreground="Black" Text="{Binding}" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Margin="5,10,0,2" FontSize="14" FontFamily="Segoe UI"/>
                        </StackPanel>
                    </ScrollViewer>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button x:Name="btnDelete" Content="Meldungen löschen" Margin="147,514,147,0" VerticalAlignment="Top" Width="122" Click="btnDelete_Click" HorizontalContentAlignment="Center" RenderTransformOrigin="0.467,0.909"/>
</Grid>

1 个答案:

答案 0 :(得分:1)

您的文本块位于stackPanel中,它不会为您的textBlock提供边界。

将父级更改为为文本块提供边界的内容或将堆栈面板本身设置为限制。

仅当文本块超出父控件的宽度时,TextWrapping才会生效。但是stackPanel从不对其子节点施加任何边界,因此所有空间都可用。

可能的解决方案是 -

<Grid Width="500" Height="Auto" Margin="0,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="380"/>
            <ColumnDefinition Width="120"/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.ColumnSpan="2">
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Foreground="Black" Text="Fehler: " HorizontalAlignment="Left" Height="Auto" Width="Auto" VerticalAlignment="Top"  Margin="5,10,0,2" FontSize="14" FontFamily="Segoe UI" ScrollViewer.VerticalScrollBarVisibility="Disabled"/>
                <TextBlock Grid.Column="1" x:Name="txtErrorLabel" 
                           Foreground="Black" 
                           TextWrapping="Wrap"
                           Text="123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789" 
                           HorizontalAlignment="Left" 
                           VerticalAlignment="Top" 
                           Margin="5,10,0,2" 
                           FontSize="14" FontFamily="Segoe UI"/>
            </Grid>
        </ScrollViewer>
    </Grid>