简体   繁体   English

WPF如何编辑以编程方式添加的DataGrid中的行

[英]WPF how to I edit rows in a DataGrid that I added programmatically C#

In my application I have an empty DataGrid with three columns created in xaml: 在我的应用程序中,我有一个空的DataGrid其中包含在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>

I am using the following code to add items (Rows) to the DataGrid : 我正在使用以下代码将项目(行)添加到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. 先感谢您。

EDIT: 编辑:

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; }

}

Well... First off you are binding to... something, but then immediately replacing that binding with a new source (your rowItems list). 好吧...首先,您要绑定到...某物,然后立即用新的源(您的rowItems列表)替换该绑定。 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. 如果您不希望尝试将应用程序制作为MVVM,只需将您在xaml中的数据列定义扔掉(网格将从您的rowItems列表中选取那些),然后在数据网格中编辑description字段时,将rowItems在您后面的代码中将被更新。 From there what you do with that list is up to you in terms of persistence or what have you. 从那里开始,您将根据持久性或拥有的内容来决定对列表的处理方式。

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. 如果要保留在MVVM中,则保留绑定并摆脱后面的代码,而将数据网格的datacontext设置为保存要绑定的对象的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? 也许添加更多代码(您的DataGridItems类是什么样?您要绑定的ViewModel是什么样?)或更多上下文?

在Datagrid的下方设置属性,完成后将其设置为True

datagrid.IsReadOnly = False;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM