WPF 数据网格行高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13589898/
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
WPF DataGrid row height
提问by Matan L
I have a datagrid in a WPF project. I've set the columns width to be have maximum limits,
and I want the data to likewise be stretched to fit in the rows. But that doesn't happen.... The contents just stays the same size and the cell is cutoff. Any ideas?
我在 WPF 项目中有一个数据网格。我已将列宽设置为具有最大限制,
并且我希望数据同样被拉伸以适应行。但这不会发生......内容只是保持相同的大小并且单元格被截断。有任何想法吗?
Here is the code :
这是代码:
<DockPanel>
<DataGrid x:Name="nirGrid" x:Uid="nirGrid" AutoGenerateColumns="False" AlternationCount="2" SelectionMode="Single" DockPanel.Dock="Top" Margin="10,50,10,50" FlowDirection="RightToLeft" ColumnWidth="SizeToCells" CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" GridLinesVisibility="None" HeadersVisibility="None" SelectionUnit="Cell" VerticalAlignment="Stretch" EnableRowVirtualization="False" IsReadOnly="True" RowDetailsVisibilityMode="Visible" MinRowHeight="0" CanUserResizeRows="True" RowHeaderWidth="0">
<DataGrid.Style>
<Style>
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
</Style>
</DataGrid.Style>
<DataGrid.Columns >
<DataGridTextColumn Binding="{Binding Path=task_desc}" IsReadOnly="True" Header="test1" Width="SizeToCells" MaxWidth="330" >
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=task_req_date}" IsReadOnly="True" Header="test2" Width="SizeToCells" MaxWidth="70" >
</DataGridTextColumn>
<DataGridTemplateColumn Header="delete" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="delete" ToolTip="delete" Opacity="0.8" Click="Button_Click" MouseEnter="Button_MouseEnter" MouseLeave="Button_MouseLeave">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Source="/exhibits;component/Images/exit1.png" Width="15" Height="15" />
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
回答by COLD TOLD
Try using the TextBlock. It should automatically wrap the content to your desired size.
尝试使用 TextBlock。它应该自动将内容包装到您想要的大小。
<DataGridTemplateColumn Width="*" Header="Column 2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=task_req_date}" TextWrapping="Wrap" AcceptsReturn="true" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

