【问题标题】:Working with WPF Form DataGrid, DataTable and Combobox使用 WPF 表单 DataGrid、DataTable 和 Combobox
【发布时间】:2014-06-23 13:32:56
【问题描述】:

我正在使用 WPF。我正在尝试使用网格在我的数据库中显示表格。目标是在该网格中拥有所有数据。示例:

ID 名字 姓氏
1 约翰·史密斯
2简·史密斯
但是,每个单元格都应该是一个组合框,如果单击,则该组合框具有该特定列的所有选项。因此,单击 John 将显示包含表中每个名字的组合框,在这种情况下,它将显示 John 和 Jane。如果用户选择点击 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&lt;string,object&gt;,它迫使您进入一个无类型、基于魔术字符串的世界和不断铸造。

标签: c# wpf datagrid combobox


【解决方案1】:

我将使用以下属性在 myDataGrid 后面创建我的数据对象

  • ObservableCollection&lt;MyObject&gt; Records
  • List&lt;int&gt; Ids
  • List&lt;string&gt; FirstNames
  • List&lt;string&gt; LastNames

然后使用具有 ComboBoxes 的 TemplateColumns 绑定我的 DataGrid,这些 ComboBoxes 绑定到 DataContext 中的值集合,如下所示:

<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 的同时是否有合适的方法来做到这一点?
猜你喜欢
  • 2011-08-23
  • 2011-10-22
  • 2013-06-30
  • 1970-01-01
  • 2022-01-11
  • 2013-02-03
  • 2011-06-27
  • 2011-08-12
相关资源
最近更新 更多