如果我有这样的列表,我们称之为Rows
,我不想添加myDataGrid.ItemsSource = Rows;
比我在所有列中得到的每个子列表的第一个myClass
看起来像
Column0 | Column1 | Column2 | Column3
firstrow0 | firstrow0 | firstrow0 | firstrow0
firstrow1 | firstrow1 | firstrow1 | firstrow1
firstrow2 | firstrow2 | firstrow2 | firstrow2
<DataGrid Name="myDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns >
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type vmv:myClass}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type vmv:myClass}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type vmv:myClass}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
var list = new List<List<myClass>>();
for (int row = 0; row < 3; row++)
{
var myRow = new List<myClass>();
for (int col = 0; col < 5; col++)
myRow.Add(new myClass() { ID = col, Name = "Row"+row +" Column:" + col });
list.Add(myRow);
}
myDataGrid.ItemsSource = list.AsEnumerable<IEnumerable>();
public class myClass
{
public int ID { get; set; }
public string Name { get; set; }
// other stuff
}
我需要什么才能让这个工作。我需要以某种方式施展它吗?我还需要其他Object
作为List<>
吗?
任何有用的东西都非常感谢!
我将无法更改DataTemplate部件
因为它是我公司创建的XAMLFile
的一部分所以它适合所有参数但原始它只适用于打印。我只将其加载到Find("ItemTemplate")
=&gt;把它当作DataTemplate
并用它来提供一个WYSIWYG
对于DataGridCell
,因为宽度和高度将不同于PrintTemplate
到PrintTemplate
以下代码是针对我的具体问题的解决方案。另请参阅michele答案
#region example Datacreation
var list = new List<IEnumerable>();
for (int row = 0; row < 5; row++)
{
var myRow = new List<myClass>();
for (int col = 0; col < 5; col++)
{
myRow.Add(new myClass() { ID = col, Name = "Row" + row + " Column:" + col });
}
list.Add(myRow);
}
#endregion
#region FileToDataTemplate
var myXamlFile = "<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' "
+ "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' "
+ "xmlns:vmv='clr-namespace:toDataGrid;assembly=toDataGrid' " //namespace
+ "SizeToContent='WidthAndHeight'>"
+ "<Window.Resources>"
+ "<DataTemplate x:Name='myFileCellTemplate' DataType='{x:Type vmv:myClass}'>"
+ "<TextBlock Text='{Binding Name}'/>"
+ "</DataTemplate>"
+ "</Window.Resources>"
// some stuff
+ "</Window>";
Window myWindow = (Window)XamlReader.Load(XmlReader.Create(new StringReader(myXamlFile)));
myWindow.Close();
DataTemplate myCellTemplate = (DataTemplate)myWindow.FindName("myFileCellTemplate");
#endregion
DataGrid myDataGrid = new DataGrid();
#region dyn DataGridcreation
for (int col = 0; col < 5; col++)
{
#region HelperDataTemplatecreation
var myResourceDictionaryString = "<ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' "
+ "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' "
+ "xmlns:vmv='clr-namespace:toDataGrid;assembly=toDataGrid'>" //namespace
+ "<DataTemplate DataType='{x:Type vmv:myClass}'>"
+ "<Label Content='{Binding [" + col + "]}'/>"
+ "</DataTemplate>"
+ "</ResourceDictionary> ";
ResourceDictionary ResDic = (ResourceDictionary)XamlReader.Load(XmlReader.Create(new StringReader(myResourceDictionaryString)));
DataTemplate HelpDTemp = (DataTemplate)ResDic[ResDic.Keys.Cast<Object>().First()];
#endregion
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = col;
templateColumn.CellTemplate = HelpDTemp;
templateColumn.CellEditingTemplate = HelpDTemp;
myDataGrid.Columns.Add(templateColumn);
}
#endregion
myDataGrid.Resources.Add(new DataTemplateKey(typeof(myClass)), myCellTemplate);
myDataGrid.ItemsSource = list.AsEnumerable<IEnumerable>();
答案 0 :(得分:1)
在我的鞋子中,我将以编程方式生成所有DataGridColumns(如注释中所示),这样您就可以为每个单元格分配正确的DataContext,并且一切都将是动态的。
但是,如果您的问题仅涉及DataBinding
问题,那么如果将TextBlock DataBinding表达式更改为:
<TextBlock Text="{Binding [0].Name}"/>
用于第一个数据模板,[1].Name
和[2].Name
用于其他两个DataTemplates。
这将是有效的,因为您的行DataContext是List<T>
,因此将[#]
添加到您的DataBinding表达式将把每个单元格的数据上下文设置为正确的对象。
编辑 - 基于以下评论:如何使用资源中的给定datattemplate以编程方式创建datagridcolumn。
代码
//In your example you have 5 columns
for (int c = 0; c < 5; c++)
{
DataGridTemplateColumn column = new DataGridTemplateColumn();
//Basically i will wrap your DataTemplate in a ContentPresenter
//The ContentProperty is set to point to the correct element of your list
var factory = new FrameworkElementFactory(typeof(ContentPresenter));
factory.SetBinding(ContentPresenter.ContentProperty, new Binding(string.Format("[{0}]", c.ToString())));
factory.SetValue(ContentPresenter.ContentTemplateProperty, this.FindResource("YourTemplateName") as DataTemplate);
column.SetValue(DataGridTemplateColumn.CellTemplateProperty, new DataTemplate { VisualTree = factory });
myDataGrid.Columns.Add(column);
}