使用WPF表单数据网格、数据表和组合框

3
我正在使用 WPF。我试图使用网格来展示数据库中的表格。目标是在该网格中拥有所有数据。例如:
ID 名字 姓氏 1 约翰 史密斯 2 简 史密斯
然而,每个单元格都应该是一个下拉框,以显示该特定列的每个选择项。因此,点击约翰将显示下拉框,并显示表格中的每个名字,在这种情况下,它将显示约翰和简。如果用户选择点击ID,则会显示1和2等。
到目前为止,我尝试使用数据表作为数据网格的项目源。这非常有效,但是我无法向数据表添加下拉框。我可以向数据网格添加下拉框列,但是这时我不再使用数据表,也不确定如何通过使用下拉框列迭代每行数据库。
因此,我想要的是在每个单元格中都有一个下拉框,用于显示该特定行的相应数据,但是点击后将列出所有可用选项。我已经搜索了一些内容,但是不确定是否正在寻找正确的内容。
我已经尝试过一些下拉框的方法,但没有什么值得注意的。另外,我生成的列都是自动生成的,不确定是否可以有非自动生成的列仍然使用绑定,或者如何定义它。
这是生成数据表的方式。
public DataTable PersonData()
{    
    List<Person> str4 = new List<Person>();
    DataTable _PersonData;

    _PersonData = new DataTable();
    _PersonData.Columns.Add(new DataColumn("FirstName", typeof(string)));
    _PersonData.Columns.Add(new DataColumn("LastName", typeof(string)));

    str4 = newquery();
    str4.ForEach(delegate(Person person1)
    {
         row3 = _PersonData.NewRow();
         _PersonData.Rows.Add(row3);
         row3["FirstName"] = person1.FirstName;
         row3["Lastname"] = person1.Lastname;
    });

    return _PersonData; 
 }

当用户在列表框中点击项目时,它会绑定数据表格。
private void youclickedon(String result)
{
     newdatatable = PersonData();
    Binding binding = new Binding() {Mode=BindingMode.OneWay, Source = newdatatable, Path = new PropertyPath(".") };
    BindingOperations.SetBinding(GridData, DataGrid.ItemsSourceProperty, binding);
    GridData.Columns[0].IsReadOnly = true;
    newdatatable.AcceptChanges();
}

我强烈建议您使用一个适当的强类型数据模型,而不是一个“DataTable”,它只是一个被夸大了的Dictionary<string,object>,会让你陷入一个无类型、基于魔术字符串的世界,并且需要频繁地进行类型转换。 - Federico Berasategui
1个回答

2

我会在DataGrid后面创建我的数据对象,并设置以下属性:

  • ObservableCollection<MyObject> Records
  • List<int> Ids
  • List<string> FirstNames
  • List<string> LastNames

然后,使用绑定到DataContext值集合的ComboBoxes,在TemplateColumns中绑定我的DataGrid,就像这样:

<DataGrid ItemsSource="{Binding Records}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Ids}"
                              SelectedItem="{Binding Id}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.FirstNames}"
                              SelectedItem="{Binding FirstName}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.LastNames}"
                              SelectedItem="{Binding LastName}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我会在网格加载时填充我的列表(如果需要,也许会随着项目的更改而更新)

Ids = Records.Select(p => p.Id).ToList();
FirstNames = Records.Select(p => p.FirstName).ToList();
LastNames = Records.Select(p => p.LastName).ToList();

有没有一种适当的方法可以在仍然使用DataTable的情况下完成这个任务? - Melissa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接