当使用列表动态填充wpf datagrid列文本时,如何包装它?
我有一个xaml,比如:当使用列表动态填充wpf datagrid列文本时,如何包装它?,wpf,datagrid,word-wrap,Wpf,Datagrid,Word Wrap,我有一个xaml,比如: <GroupBox Header="groupBox1" Height="174" HorizontalAlignment="Left" Margin="36,38,0,0" Name="groupBox1" VerticalAlignment="Top" Width="285"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility
<GroupBox Header="groupBox1" Height="174" HorizontalAlignment="Left" Margin="36,38,0,0" Name="groupBox1" VerticalAlignment="Top" Width="285">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid x:Name="caseListGrid">
</Grid>
</ScrollViewer>
</GroupBox>
在.cs文件中,我有一个函数:
private void LoadDataGrid()
{
caseElementList = new List<CaseElement>();
caseElementList.Clear();
//add 1st element
caseElementList.Add(new CaseElement
{
CaseName = "First Name",
Description = "Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox"
});
//add second
caseElementList.Add(new CaseElement
{
CaseName = "Second Name",
Description = "Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox"
});
caseElementList.Add(new CaseElement
{
CaseName = "Third Name",
Description = "Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox"
});
DataGrid dGrid = new DataGrid();
dGrid.AutoGenerateColumns = true;
dGrid.ItemsSource = caseElementList;
this.caseListGrid.Children.Add(dGrid);
}
在函数中,我创建了自定义案例对象的列表。然后,在创建datagrid之后,我将列表分配给datagrid
但是因为我没有在datagrid中添加样式。。而且文本非常大。所以
它正在滚动视图中展开
但是我想使用文本换行来显示它们。那么,谁能给我任何解决办法
我发现使用textblock可以解决这个问题。但是我不知道如何在数据网格中添加文本块,因为我是动态创建的 解决方案1:不要自动生成列并自己指定列
解决方案2:订阅事件并将列更改为包装的内容,例如使用列的属性。我创建了一个函数,用于接收事件自动生成列的eventhandler DataGridAutoGeneratingColumnEventArgs e。这是我的新代码:private void AutoGeneratingColumnEventHandlerobject sender,DataGridAutoGeneratingColumnEventArgs e{string header=e.Column.header.ToString;MessageBox.ShowHeader::+header;}但我只能保存头。但不是其他行列表。。。你能给我一些建议,我该如何写这些风格。。。