wpf TextBlock TextWrapping 不换行 #2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16221073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:41:13  来源:igfitidea点击:

TextBlock TextWrapping not wrapping #2

wpfxamltextblockword-wrap

提问by Developer

OK... so thissolution doesn't help

好的...所以这个解决方案没有帮助

XAML is here

XAML 在这里

  <ListBox  ItemsSource="{Binding Path=ContentItems}" Background="White" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="2" Height="Auto" Width="Auto">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid Grid.Column="0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <StackPanel Grid.Row="0" Orientation="Horizontal">
                                    <Label VerticalAlignment="Center"  Margin="0,0,0,5">Start</Label><svl:TimeEditor Value="{Binding Path=FormatedStart}" Width="87"  HorizontalAlignment="Left"  Margin="2,8"  Name="dtpStart" FontSize="12"  Height="25"  VerticalAlignment="Center"     />
                                    <Label VerticalAlignment="Center"  Margin="0,0,0,5">End</Label><svl:TimeEditor Value="{Binding Path=FormatedEnd}" Width="87"  HorizontalAlignment="Left"  Margin="2,8"  Name="dtpEnd" FontSize="12"  Height="25"  VerticalAlignment="Center"     />
                                </StackPanel>                               
                                <TextBlock Grid.Row="1"  TextWrapping="Wrap"  Name="tbText" Text="{Binding Path=Data}"></TextBlock>
                            </Grid>
                            <Grid Grid.Column="1" Visibility="Collapsed">
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

回答by Nandha kumar

The following will helps for text wrapping:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">

回答by Viv

Well your TextBlockdoes not need to wrap since your specifying Widthas Autofor it's ColumnDefinitionwhich allows it to take all the Widthit needs to fit Content even at the cost of overflowing. You either need to set the Column's Width to "*" to allow the TextWrappingto kick in when requested width exceeds allowable or manually force a MaxWidthon it using a Binding like

嗯,你TextBlock不需要因为你的指定包装Width作为Auto它的ColumnDefinition允许它把所有的Width需要,即使在四溢的成本,以适应内容。您需要将列的宽度设置为“*”以允许TextWrapping在请求的宽度超过允许时启动,或者MaxWidth使用 Binding手动强制 a在它上面

<TextBlock Name="tbText" Grid.Row="1" MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualWidth}" Text="{Binding Path=Data}" TextWrapping="Wrap" />