简体   繁体   English

StackPanel(WP7)内的文字换行

[英]Text wrapping inside StackPanel (WP7)

I'd like to wrap text contained in three TextBlock s inside a StackPanel without writing TextWrapping="Wrap" for each TextBlock (sometimes there may be more of those): 我想将三个TextBlock包含的文本包装在StackPanel而不必为每个TextBlock编写TextWrapping="Wrap" (有时可能会有更多):

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name, Mode=OneWay}" TextWrapping="Wrap" />
                    <TextBlock Text="{Binding Path=Distance, Mode=OneWay}" TextWrapping="Wrap" />
                    <TextBlock Text="{Binding Path=Description, Mode=OneWay}" TextWrapping="Wrap" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

So if I dynamically add another TextBlock it should wrap automatically (I don't want to do it inside my code-behind file) 因此,如果我动态添加另一个TextBlock它应该自动换行(我不想在我的代码隐藏文件中这样做)

In other words - I'd like to write style that would be automatically applied. 换句话说-我想写一种可以自动应用的样式。 In CSS it would be something like that: 在CSS中将是这样的:

listbox textblock {
    word-wrap:break-word;
}

UPDATE UPDATE

This contains my ListBox: 这包含我的ListBox:

<Grid Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Grid.Column="0" Margin="12,0,12,0">
            <views:ListItem Margin="12,6,0,0" />
        </Grid>
    </Grid>

Unless you specify a Width constraint for the TextBlock , it will not wrap its text content. 除非您为TextBlock指定一个Width约束,否则它不会包装其文本内容。 As the StackPanel does not resize its contents, it will never pass any Width constraints to the TextBlock s inside and so they will never wrap. 由于StackPanel 调整它的内容,它永远不会通过任何Width限制到TextBlock的内线,所以他们永远不会换行。 Setting the TextWrapping property to Wrap is not enough to make the text content wrap, so applying a Style with this property set will not do what you want. 设置TextWrapping属性Wrap 不足以使文本内容换行,所以应用Style带有此属性不会做你想要的。

Did you try adding a style for TextBlock ? 您是否尝试为TextBlock添加样式? Ie

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="TextWrapping" Value="Wrap"/>
                </Style>
            </StackPanel.Resources>
            ...
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM