我希望使用一列代表相当冗长的文本值来创建数据网格。所以我的目标功能是:
以下代码符合前两项并提供了正常运行的垂直滚动条,但数据网格高度对于显示的内容来说太奇怪了。从文本块中删除自动换行修复了这个...但我需要自动换行。
如何在不使数据网格高度过大的情况下保留自动换行功能?
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- other controls in different parts of the data grid -->
<DataGrid Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
Margin="0,6,6,6" Name="dgMessages" VerticalAlignment="Top"
Background="DarkGray" HeadersVisibility="None"
AlternatingRowBackground="Gainsboro" CanUserResizeColumns="False"
CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="false" BorderBrush="Black" HorizontalGridLinesBrush="{x:Null}"
ItemsSource="{Binding Messages, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<DataGrid.Columns>
<dg:DataGridTemplateColumn Width="*">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"
TextWrapping="WrapWithOverflow"
Padding="5,5,5,5" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>
&#34; DG&#34;命名空间是&#34; http://schemas.microsoft.com/winfx/2006/xaml/presentation"
答案 0 :(得分:1)
删除滚动查看器并将包含数据网格的行的高度设置为“*”修复此问题。当数据网格不在自动调整为内容大小的行中时,通常会处理数据网格列中的包装文本。
以下是结束代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- other controls in different parts of the data grid -->
<DataGrid Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
Margin="0,6,6,6" Name="dgMessages" VerticalAlignment="Top"
Background="DarkGray" HeadersVisibility="None"
AlternatingRowBackground="Gainsboro" CanUserResizeColumns="False"
CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="false" BorderBrush="Black" HorizontalGridLinesBrush="{x:Null}"
ItemsSource="{Binding Messages, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<DataGrid.Columns>
<dg:DataGridTemplateColumn Width="*">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" TextWrapping="WrapWithOverflow" Padding="5,5,5,5" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>