WPF如何编辑以编程方式添加的DataGrid中的行 [英] WPF how to I edit rows in a DataGrid that I added programmatically C#

查看:203
本文介绍了WPF如何编辑以编程方式添加的DataGrid中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个空的 DataGrid ,其中包含在xaml中创建的三列:

In my application I have an empty DataGrid with three columns created in xaml:

<DataGrid.Columns>
            <DataGridTextColumn Header="Step Number" Binding="{Binding StepNumber}" />
            <DataGridTextColumn Header="Requirement" Binding="{Binding Requirement}" />
            <DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="*" />
</DataGrid.Columns>

我正在使用以下代码将项目(行)添加到 DataGrid

I am using the following code to add items (Rows) to the DataGrid:

 Dictionary<string, List<int>> reqLocations = modifier.ValidateRequirements(col, startRow, RequirementCallbackHandler, CredentialHandler);

  List<DataGridItems> rowItems = new List<DataGridItems>();

  foreach (KeyValuePair<string, List<int>> kvp in reqLocations)
  {
    // Create Rows
    rowItems.Add(new DataGridItems() { StepNumber = ReturnStepNumber(kvp.Value[0]), Requirement = kvp.Key, Description = "Loading Requirement..." });
  }

  // Add Items source to DataGrid
  dataGrid.ItemsSource = rowItems ;

在运行时,我需要编辑这些行以填充描述列。我该如何实现?

During runtime I need to edit these rows to fill in the description column. how can I achieve this? thank you in advance.

编辑:

要清楚,我需要通过代码而不是手动编辑这些行。

To be clear I need to edit these rows through code not manually.

对于要求我包含更多代码的人,这里只是一小段代码:

For the person that asked me to include more code, its just a small class here is the code:

public class DataGridItems
{
  public string StepNumber { get; set; }
  public string Requirement { get; set; }
  public string Description { get; set; }
  public ValidityState state { get; set; }
  public HUDI.IJMPSRequirement req { get; set; }

}


推荐答案

...首先,您要绑定到...某物,然后立即用新的源(您的rowItems列表)替换该绑定。如果您不希望尝试使应用程序成为MVVM,只需将您在xaml中的数据列定义扔掉(网格将从您的rowItems列表中选取那些),然后在数据网格中编辑description字段时,将rowItems在您后面的代码中将被更新。从那里开始,您将根据持久性或拥有的内容来决定使用该列表。

Well... First off you are binding to... something, but then immediately replacing that binding with a new source (your rowItems list). If you don't care to try and make your application MVVM, just throw away you data columns definition in the xaml (the grid will pick those up from your rowItems list) and then when you edit your description field in the data grid the rowItems in your code behind will be updated. From there what you do with that list is up to you in terms of persistence or what have you.

如果您想留在MVVM领域,请保留绑定并获取删除后面的代码,而是将数据网格的datacontext设置为包含要绑定的对象的ViewModel。

If you want to stay in MVVM land then keep the bindings and get rid of the code behind and instead set the datacontext of your data grid to the ViewModel that is holding the things you are binding to.

也许添加更多代码(做什么您的DataGridItems类是什么样的?您要绑定的ViewModel是什么样的?)或更多上下文?

Maybe add some more code (what does your DataGridItems class look like? What does the ViewModel you are binding to look like?) or some more context?

这篇关于WPF如何编辑以编程方式添加的DataGrid中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆