WPF .Net 4.0 MVVM 将 DataGrid 单元绑定到数组元素对象
我正在创建一个时间表应用程序,其中有一个员工列表以及要分配时间的编码列表。
我创建了一个 DataMatrix 并且网格看起来很好,除了小时的数据输入网
格看起来像
Work Coding | AL | Sick | Job1 | Job2
____________________________________________
Employee1 | | | |
Employee2 | | | |
public class DataMatrix : IEnumerable
{
public List<MatrixColumn> Columns { get; set; }
// public List<object[]> Rows { get; set; }
public List<TimesheetDetail[]> Rows { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return new GenericEnumerator(Rows.ToArray());
}
}
数据网格 ItemsSource 是 TimesheetArray。
我遇到的问题是,当我输入 Employee1 Job1 的数据时,数据网格看起来像
Work Coding | AL | Sick | Job1 | Job2
____________________________________________
Employee1 | 2 | 2 | 2 | 2
我想要的地方,
Work Coding | AL | Sick | Job1 | Job2
____________________________________________
Employee1 | | | 2 |
数据模板看起来像
<DataTemplate x:Key="TimesheetEntryDetailCellTemplate"
DataType="{x:Type data:TimesheetDetail}">
<Grid>...
<Label Content="ST" />
<TextBox x:Name="txtStandardTime"
Text="{Binding Path=HoursWorked, ...}"></TextBox>
</Grid>
</DataTemplate>
通过调试,我得到了一个要绑定的 TimesheetDetail[] 对象,
我认为我需要类似的东西
<TextBox Text="{Binding Source = TimesheetDetail[ColumnDisplayIndex].HoursWorked}" />
有人知道吗我怎样才能让单元格模板绑定到它所挂接的元素???
提前致谢
I am Creating a Timesheet application where there is a list of employees together with a list of Codings to assign time to.
I have Created a DataMatrix And I have Got the grid looking Just fine EXCEPT the data entry of the hours
The grid looks something like
Work Coding | AL | Sick | Job1 | Job2
____________________________________________
Employee1 | | | |
Employee2 | | | |
public class DataMatrix : IEnumerable
{
public List<MatrixColumn> Columns { get; set; }
// public List<object[]> Rows { get; set; }
public List<TimesheetDetail[]> Rows { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return new GenericEnumerator(Rows.ToArray());
}
}
The datagrid ItemsSource is TimesheetArray.
The Issue I am having is when I enter data for Employee1 Job1 the datagrid looks like
Work Coding | AL | Sick | Job1 | Job2
____________________________________________
Employee1 | 2 | 2 | 2 | 2
Where I Want it looking like
Work Coding | AL | Sick | Job1 | Job2
____________________________________________
Employee1 | | | 2 |
The Data Template looks like
<DataTemplate x:Key="TimesheetEntryDetailCellTemplate"
DataType="{x:Type data:TimesheetDetail}">
<Grid>...
<Label Content="ST" />
<TextBox x:Name="txtStandardTime"
Text="{Binding Path=HoursWorked, ...}"></TextBox>
</Grid>
</DataTemplate>
With debugging I am getting a TimesheetDetail[] object to bind to
I think I need something like
<TextBox Text="{Binding Source = TimesheetDetail[ColumnDisplayIndex].HoursWorked}" />
Does anyone know how I can get the cell template to bind to the element it is hooked to???
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是通过代码创建动态绑定表达式并生成数据网格列(xaml 中的 AutoGenerateColumns="False"),apporx。下面给出的代码(未编译):
public void AddColumns(string[] myHeaderList, List
{
for (int i = 0; i < myHeaderList.Length; i++)
{
DataGridTemplateColumn gridCol = new DataGridTemplateColumn();
gridCol.Header = myHeaderList[i];
gridCol.CellTemplate =
(DataTemplate)this.Resources["TimesheetEntryDetailCellTemplate"];
gridCol.Binding = new Binding("[" + i + "].HoursWorked");
_dataGrid.Columns.Add(gridCol);
}
}myList=...;
列表
_dataGrid.ItemsSource = myList;
在 XAML 中,您需要将 AutoGenerateColumns 设置为 false:
< DataGrid名称=“_dataGrid”AutoGenerateColumns=“False”...>
One way is to create dynamic binding expression through the code and generating your datagrid columns (AutoGenerateColumns="False" in xaml), apporx. code given below (not compiled):
public void AddColumns(string[] myHeaderList, List<TimesheetDetail[]> myList)
{
for (int i = 0; i < myHeaderList.Length; i++)
{
DataGridTemplateColumn gridCol = new DataGridTemplateColumn();
gridCol.Header = myHeaderList[i];
gridCol.CellTemplate =
(DataTemplate)this.Resources["TimesheetEntryDetailCellTemplate"];
gridCol.Binding = new Binding("[" + i + "].HoursWorked");
_dataGrid.Columns.Add(gridCol);
}
}
List<TimesheetDetail[]> myList=...;
_dataGrid.ItemsSource = myList;
In XAML you need to set AutoGenerateColumns to false:
< DataGrid Name="_dataGrid" AutoGenerateColumns="False" ...>