ListView中的网格中的WPF TextBlock没有正确调整大小?

时间:2009-08-28 08:33:32

标签: wpf listview grid textwrapping column-width

我有一个TextBlock,它位于Grid中,是ListView的ItemTemplate。我有这些项目,以便在调整窗口大小时它们会增长,但我无法弄清楚如何将TextBlock限制为该大小。我试图用ColumnDefinition的宽度来做这个 - 如果我将Width设置为固定数字(比如350),文本正确包装,但显然当窗口展开时TextBlock不会展开 - 如果我设置了宽度为“*”,然后有一个水平滚动条,文本向右移动,不会换行。

知道我在这里做错了吗?

<GroupBox Header="Urgent Items" Margin="8,8,8,340" Name="UrgetItemsGroupBox">
    <Grid>
        <ListView Margin="6" Name="CriticalErrorsListView" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Path=.}" MouseDoubleClick="CriticalErrorsListView_MouseDoubleClick">
            <ListView.Background>
                <LinearGradientBrush EndPoint="-0.192,0.529" StartPoint="0.998,0.519">
                    <GradientStop Color="#FFD2D2D2" Offset="0"/>
                    <GradientStop Color="#FFFFFFFF" Offset="1"/>
                </LinearGradientBrush>
            </ListView.Background>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Border Margin="2,2,2,3" BorderBrush="#FF847F6E" CornerRadius="10" BorderThickness="3">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30" />
                                <ColumnDefinition Width="10" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="25" />
                                <RowDefinition Height="20" />
                                <RowDefinition Height="20" />
                                <RowDefinition Height="75" />
                            </Grid.RowDefinitions>
                            <Image Grid.Row="0" Grid.RowSpan="5" Grid.Column="0" Margin="2,2,2,2" Source="Images\errorIcon.png" />
                            <TextBlock Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Margin="2,2,2,2" Text="{Binding Path=ApplicationName}" FontFamily="Calibri" FontWeight="Bold" FontSize="18" />
                            <TextBlock Grid.Row="1" Grid.Column="2" Margin="10,2,10,2" Text="{Binding Path=ErrorTime}" FontFamily="Calibri" FontSize="12" />
                            <TextBlock Grid.Row="2" Grid.Column="2" Margin="10,2,10,2" Text="{Binding Path=ErrorPerson}" FontFamily="Calibri" FontSize="12" />
                            <TextBlock Grid.Row="3" Grid.Column="2" Margin="2,2,2,2" Text="{Binding Path=ShortDescription}" TextWrapping="Wrap" />
                        </Grid>
                    </Border>                            
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</GroupBox>

1 个答案:

答案 0 :(得分:2)

您会看到水平滚动条,因为ListView在其模板中使用ScrollViewer来允许滚动。您需要做的就是让ScrollViewer不要水平滚动。只需在ListView上设置 ScrollViewer.Horizo​​ntalScrollBarVisibility =“已禁用”即可。所以你会有这样的事情:

<ListView Margin="6"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          Name="CriticalErrorsListView"
          HorizontalContentAlignment="Stretch"
          ItemsSource="{Binding Path=.}"
          MouseDoubleClick="CriticalErrorsListView_MouseDoubleClick">
     <!-- The rest goes here. -->
</ListView>

希望这有帮助。