社区
其他技术讨论专区
帖子详情
WPF DataGrid 自动换行
weixin_38051479
2019-06-08 02:42:35
new SqlDataAdapter().Fill(dataTable);
System.Windows.Controls.DataGrid.ItemsSource = dataTable.DefaultView;
通过这种方式将 DataTable 绑定到 DataGrid 中并显示,如何让单元格中,内容超出列宽的文本自动换行。
...全文
415
4
打赏
收藏
WPF DataGrid 自动换行
new SqlDataAdapter().Fill(dataTable); System.Windows.Controls.DataGrid.ItemsSource = dataTable.DefaultView; 通过这种方式将 DataTable 绑定到 DataGrid 中并显示,如何让单元格中,内容超出列宽的文本自动换行。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
weixin_38102396
2019-06-10
打赏
举报
回复
那就用 event 來做
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var table = new DataTable(); table.Columns.Add("Description"); table.Columns.Add("Name"); for(int i =0; i < 10; i ++) { var row = table.NewRow(); row[0] = $"PPPPPPP {i}, KKKKKKKKKK {i}"; row[1] = i.ToString(); table.Rows.Add(row); } dataGrid.ItemsSource = table.DefaultView; } private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.Column is DataGridTextColumn column) { var style = new Style(typeof(TextBlock)); style.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap)); column.ElementStyle = style; } } }
<Window x:Class="WpfDataGridTextWrappingSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfDataGridTextWrappingSample" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <DataGrid AutoGenerateColumns="True" x:Name="dataGrid" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" /> </Window>
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。 https://skilltree.my/
weixin_38069146
2019-06-10
打赏
举报
回复
那就用 event 來做
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var table = new DataTable(); table.Columns.Add("Description"); table.Columns.Add("Name"); for(int i =0; i < 10; i ++) { var row = table.NewRow(); row[0] = $"PPPPPPP {i}, KKKKKKKKKK {i}"; row[1] = i.ToString(); table.Rows.Add(row); } dataGrid.ItemsSource = table.DefaultView; } private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.Column is DataGridTextColumn column) { var style = new Style(typeof(TextBlock)); style.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap)); column.ElementStyle = style; } } }
<Window x:Class="WpfDataGridTextWrappingSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfDataGridTextWrappingSample" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <DataGrid AutoGenerateColumns="True" x:Name="dataGrid" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" /> </Window>
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。 https://skilltree.my/
weixin_38101966
2019-06-09
打赏
举报
回复
你好,DataTable 中的内容是未知的,无法提前写好一个类,及在 xaml 中写好 DataGridTextColumn。
如果是这种方式,我是否需要这样做:
在后台遍历 DataTable,自动创建 DataGridTextColumn 并添加到 DataGrid.Columns.Items 中。
weixin_38096180
2019-06-08
打赏
举报
回复
<DataGrid Margin="16" ItemsSource="{Binding People}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="姓名" Width="80" Binding="{Binding Name, Mode=TwoWay }"> <DataGridTextColumn.ElementStyle > <Style TargetType="TextBlock" > <Setter Property="Foreground" Value="Blue"/> <Setter Property="TextWrapping" Value="Wrap" /> </Style> </DataGridTextColumn.ElementStyle> <DataGridTextColumn.EditingElementStyle > <Style TargetType="TextBox" > <Setter Property="Foreground" Value="Red"/> <Setter Property="TextWrapping" Value="Wrap" /> </Style> </DataGridTextColumn.EditingElementStyle> </DataGridTextColumn>
自訂 Element Style 類似上方的 xaml
在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。 https://skilltree.my/
WPF
中的
DataGrid
实现单元格内容
自动换行
博客介绍了
WPF
中
DataGrid
实现单元格内容
自动换行
的两种方式。第一种用ElementStyle,需手动设置行高度,缺点是每行高度固定;第二种用DataTemplate,能根据内容多少自动设置高度并实现换行,推荐使用第二种方式。
WCF
DataGrid
列的
自动换行
本文介绍如何在
WPF
DataGrid
的列中通过设置TextBlock的样式来实现文本
自动换行
,并确保列高自动调整以适应内容。具体方法是在
DataGrid
TextColumn中定义ElementStyle,将TextWrapping设为Wrap并设置Height为auto。
silverlight/
wpf
通过后台创建
datagrid
模版列
本文介绍了一种在
Datagrid
中处理长文本
自动换行
的方法,通过自定义模板列和TextBlock组件,实现了文本的美观展示,适用于
WPF
应用程序。
DataGrid
实现内容
自动换行
本文介绍两种在
WPF
中使
DataGrid
的单元格内容能够换行的方法:一是通过ElementStyle设置TextBlock的TextWrapping属性;二是使用DataTemplate结合TextBlock实现
自动换行
并调整行高。
[学习笔记]
WPF
设置
DataGrid
行内容高度自适应
通过在
WPF
的
DataGrid
中使用模板列和TextBlock的TextWrapping属性,可以实现
DataGrid
行内容的高度自适应。具体做法是将
DataGrid
TextColumn替换为
DataGrid
TemplateColumn,并设置TextBlock的TextWrapping为Wrap,同时调整列的宽度。这样,内容过长的单元格会
自动换行
,确保行高适应内容。
其他技术讨论专区
477
社区成员
790,950
社区内容
发帖
与我相关
我的任务
其他技术讨论专区
其他技术讨论专区
复制链接
扫一扫
分享
社区描述
其他技术讨论专区
其他
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章