C# 绑定到 XmlDataProvider 时,如何在 WPF DataGrid 中创建新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/480098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:02:59  来源:igfitidea点击:

How do I create a new row in WPF DataGrid when it is bound to an XmlDataProvider?

c#wpfwpftoolkitxmldataprovider

提问by bluepolystyreneman

I have a project with an XmlDataProvider bound to a WPF DataGrid control. I have the bindings on the DataGrid set up as follows:

我有一个 XmlDataProvider 绑定到 WPF DataGrid 控件的项目。我在 DataGrid 上的绑定设置如下:

<dg:DataGrid ItemsSource="{Binding Source={StaticResource XmlData}, XPath=Root/People/Person}"
             AutoGenerateColumns="False">
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Header="ID" Binding="{Binding XPath=ID}"/>
        <dg:DataGridTextColumn Header="Name" Binding="{Binding XPath=Name}"/>
    </dg:DataGrid.Columns>
</dg:DataGrid>

Users can edit entries using the DataGrid without any problems. What I cannot manage to accomplish is allowing the user to add a new row (i.e. a new Person) using the DataGrid. How can I allow this?

用户可以使用 DataGrid 编辑条目而不会出现任何问题。我无法完成的是允许用户使用 DataGrid 添加新行(即新人员)。我怎么能允许这个?

回答by user61073

Have you tried setting CanUserAddRows="True" on the DataGrid?

您是否尝试过在 DataGrid 上设置 CanUserAddRows="True" ?

回答by Brett Bim

Is the problem that the user can't add rows or is it that when the user does add a row, it's not saved to the backing XML store? I can easily add a datagrid with CanUserAddRows="True" to a WPF application, bind the grid to an in-memory list and then have the user add rows that are reflected in that in-memory list. That makes me think that your problem is saving to the backing store.

问题是用户无法添加行还是当用户添加行时,它没有保存到支持的 XML 存储中?我可以轻松地将带有 CanUserAddRows="True" 的数据网格添加到 WPF 应用程序,将网格绑定到内存列表,然后让用户添加反映在该内存列表中的行。这让我认为您的问题是保存到后备商店。

When I bind to an XML on the file system, I can no longer add records to the data grid. I think you will need a minor workaround in that you read the file into an in-memory collection, bind to that and then update the file accordingly as users add rows.

当我绑定到文件系统上的 XML 时,我无法再向数据网格添加记录。我认为您需要一个小的解决方法,将文件读入内存中的集合,绑定到该集合,然后在用户添加行时相应地更新文件。

回答by Boris Lipschitz

Make sure that you set: CanUserAddRows="True"and that the default constructor of a bound class is available.

确保您设置了:CanUserAddRows="True"并且绑定类的默认构造函数可用。

回答by Timothy Lee Russell

To add a row to a WPF DataGrid that is bound to an XmlDataSource, you need to directly modify the backing data store. You can use the DataGrid to collect the new row information from the user and then in the RowEditEndingevent, you can add the row's information to your backing store and prevent the DataGrid from actually trying to commit the edit using its internal logic. Since the DataGrid is bound to the XmlDataSource, it will display the changes you made to the backing store.

要将行添加到绑定到 XmlDataSource 的 WPF DataGrid,您需要直接修改后备数据存储。您可以使用 DataGrid 从用户那里收集新的行信息,然后在RowEditEnding事件中,您可以将行的信息添加到您的后备存储中,并防止 DataGrid 实际尝试使用其内部逻辑提交编辑。由于 DataGrid 绑定到 XmlDataSource,它将显示您对后备存储所做的更改。

Here is the general idea:

这是一般的想法:

private void MyDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
  if (e.EditAction == DataGridEditAction.Cancel)
  {
    e.Cancel = false;
    return;
  }

  if (e.EditAction == DataGridEditAction.Commit)
  {
    DataGridRow dgr = e.Row;
    XmlElement xe = myXmlDataProvider.Document.CreateElement("NewRowElement");
    foreach(DataGridCell cell in dgr.Cells)
    {
      xe.SetAttribute(cell.Name, cell.Value);
    }
    dataProvider.Document.DocumentElement.AppendChild(xe);
    e.Cancel = true;
  }
}