无法让Xaml TextBlock TextWrapping工作

时间:2017-07-03 09:17:42

标签: wpf xaml textblock textwrapping

我的代码很简单:

<ListView Grid.Row="0" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Grid.Row="0" Margin="0 2 0 2" BorderBrush="Black" BorderThickness="1" x:Name="ListItem" >
                <Grid Margin="5">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Title}" FontWeight="Bold" FontSize="18" />
                    <Image Grid.Row="0" Grid.Column="1" Source="../Images/x.png" HorizontalAlignment="Right"/>
                    <StackPanel Orientation="Vertical" Grid.Row="1" Grid.ColumnSpan="2" MaxWidth="{Binding ElementName=ListItem, Path=Width}">
                        <TextBlock TextWrapping="WrapWithOverflow" Text="{Binding Description}" />
                    </StackPanel>
                    <TextBlock Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding TimeStamp}" HorizontalAlignment="Right"/>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

但是每次有一个带有长描述的项目时,列表视图宽度会变得大于窗口的宽度

Horizo​​ntalContentAlignment设置器,TextWrapping或MaxWitdh都不起作用。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

禁用水平滚动:

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...>