我有一个绑定到Assignments
的{{1}}列表,这会产生每个DataGrid
的行。
现在我想将每个Assignment
元素绑定到它自己的列。我如何通过DataBinding实现这一点?
Motors
网格最终应该看起来像那样(列不需要动态生成):
public class Motor {
public string Name { get; set; }
}
public class Assignment {
public string Name { get; set; }
public ObservableCollection<Motor> Motors { get; set; }
}
public class AssignmentViewModel {
public ObservableCollection<Assignment> Assignments{ get; set; }
}
<Window xmlns:vm="clr-namespace:MyMotors.ViewModel">
<Window.DataContext>
<vm:AssignmentViewModel/>
</Window.DataContext>
<DataGrid ItemsSource="{Binding Assignments}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name"/>
<DataGridTemplateColumn Header="Motor0"/>
<DataGridTemplateColumn Header="Motor1"/>
<DataGridTemplateColumn Header="Motor2"/>
<DataGridTemplateColumn Header="Motor3"/>
</DataGrid.Columns>
</DataGrid>
</Window>
目前,每列都有一个自己的CellTemplate,它直接绑定到这样的值:
+---------------+-----------+-----------+-----------+-----------+
| Name | Motor0 | Motor1 | Motor2 | Motor3 |
+---------------+-----------+-----------+-----------+-----------+
+---------------+-----------+-----------+-----------+-----------+
| Assignment0 | Motor00 | Motor10 | Motor20 | Motor30 |
+---------------+-----------+-----------+-----------+-----------+
| Assignment1 | Motor01 | Motor11 | Motor21 | Motor31 |
+---------------+-----------+-----------+-----------+-----------+
| Assignment2 | Motor02 | Motor12 | Motor22 | Motor32 |
+---------------+-----------+-----------+-----------+-----------+
但如果我想添加新列,那就太乏味了。
答案 0 :(得分:0)
您可以使用绑定到select
集合的不同索引的DataGridTextColumns
:
Motors