ListView 网格中的 WPF TexBlock 大小调整不正确?
我有一个位于网格中的 TextBlock,它是 ListView 的 ItemTemplate。我有这些项目,以便在调整窗口大小时它们会增长,但我不知道如何将 TextBlock 限制为该大小。我尝试使用 ColumnDefinition 上的宽度来执行此操作 - 如果我将宽度设置为固定数字(例如 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>
I have a TextBlock that is in a Grid that is an ItemTemplate for a ListView. I have the items so that they grow when the window is resized, but I cannot figure out how to have the TextBlock be limited to that size. I've tried to do this with the width on the ColumnDefinition - if I set the Width to a fixed number (say 350) the text wraps correctly, but obviously the TextBlock doesn't expand when the window is expanded - if I set the Width to "*" the there is then a horizontal scroll bar and the text runs off to the right and doesn't wrap.
Any idea what I'm doing wrong here?
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您会看到水平滚动条,因为 ListView 在其模板中使用 ScrollViewer 来允许滚动。您需要做的就是说 ScrollViewer 不要水平滚动。只需在 ListView 上设置 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 即可。所以你会得到这样的东西:
希望这有帮助。
You see horizontal scroll bar because ListView uses ScrollViewer in its template to allow scrolling. All you need to do is say ScrollViewer to not scroll horizontally. Just set ScrollViewer.HorizontalScrollBarVisibility="Disabled" on your ListView. So you'll have something like this:
Hope this helps.