wpf 可编辑的 DataGrid - CanUserAddRows="True" 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22330173/
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-09-13 11:11:38  来源:igfitidea点击:

Editable DataGrid - CanUserAddRows="True" not working

wpfdatagriddatagridtemplatecolumn

提问by Lucifer

I have the following DataGrid:

我有以下数据网格:

 <DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True" AutoGenerateColumns="False" Margin="0,0,0,90">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="CountryCombo2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
                                      DisplayMemberPath="CountryName" 
                                      SelectedItem="{Binding EmployeeCountry, Mode=TwoWay}"
                                      SelectedValue="{Binding EmployeeCountry.CountryId}"
                                      SelectedValuePath="CountryId" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

However, I am unable to add new rows to the DataGrid. Please let me know if I need to provide any additional code.

但是,我无法向 DataGrid 添加新行。如果我需要提供任何其他代码,请告诉我。

Update :

更新 :

Screen 1 : This is the screenshot when the window is just loaded with the hardcoded property values. Now I see the empty new row.

屏幕 1:这是窗口刚加载硬编码属性值时的屏幕截图。现在我看到了空的新行。

Screen 1

屏幕 1

Screen 2 : Here I have added data into the new row with values Rambo and Russia. Now, no matter what I do (tab-out, click in another cell), the next new row is not added. I believe it should be adding a new row.

屏幕 2:在这里,我已将数据添加到新行中,其值为 Rambo 和 Russia。现在,无论我做什么(跳出,单击另一个单元格),都不会添加下一个新行。我相信它应该添加一个新行。

Screen 2

屏幕 2

Screen 3 : Here the newly added row values have disappeared. That is because I double clicked on the thin border between the two empty cells. Now this is pretty weird.

屏幕 3:此处新添加的行值已消失。那是因为我双击了两个空单元格之间的细边框。现在这很奇怪。

Screen 3

屏幕 3

Screen 4 : Now when I click on the Peter cell, the previously entered row data is back but now it is pushed down and a new empty row is inserted before it. This is very strange.

屏幕 4:现在当我点击 Peter 单元格时,之前输入的行数据又回来了,但现在它被向下推了,并在它之前插入了一个新的空行。这很奇怪。

Screen 3

屏幕 3

Can anyone please help me understand this behavior of the DataGrid.

任何人都可以帮助我理解 DataGrid 的这种行为。

回答by yu yang Jian

In my case,

就我而言,

First ensure your ItemSourceis not using an arraythat can't add new item to it,

首先确保您ItemSource没有使用array无法向其添加新项目的

use something like Listthat can add newItem,

使用类似的东西List可以添加newItem,

Besides, the SomeClassshould have an default constructortakes no parameters like

此外,SomeClass应该有一个默认构造函数,不带参数,如

List<SomeClass>();

public Class SomeClass
{       
  public SomeClass() { }
}

then the new empty row appear in the bottom of the DataGrid.

然后新的空行出现在DataGrid.

Refer to thisanswer.

参考这个答案。

回答by ouflak

I'm going ahead and posting this as an answer here as I need to post a code sample and the comments are starting to become extended (got the invite-to-chat message).

我将继续并在此处发布此作为答案,因为我需要发布代码示例并且评论开始扩展(收到聊天邀请消息)。

The answer to the original question was ensure that the Type T of the ItemsSource had a parameterless constructor.

原始问题的答案是确保 ItemsSource 的 Type T 具有无参数构造函数。

Try this code attached to the DataGrid's BeginningEdit event to swallow up the cell border clicks:

尝试将此代码附加到 DataGrid 的 BeginningEdit 事件以吞噬单元格边框点击:

private void Grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    //// Have to do this in the unusual case where the border of the cell gets selected
    e.Cancel = true;
}

If you are actually using this handler for something else, or intend to, you can check the OriginalSource to see if it is a Border and cancel the event on that condition.

如果您实际上将此处理程序用于其他用途,或打算用于其他用途,您可以检查 OriginalSource 以查看它是否为 Border 并在该条件下取消事件。

回答by Mentor

Use DataGridTextColumn and DataGridComboBoxColumn instead DataGridTemplateColumn, then rows will be added by adequately.

使用 DataGridTextColumn 和 DataGridComboBoxColumn 代替 DataGridTemplateColumn,然后行将被充分添加。

If you want to use DataGridTemplateColumn, then set not only the CellTemplate, but CellEditingTemplate. For example:

如果要使用DataGridTemplateColumn,那么不仅要设置CellTemplate,还要设置CellEditingTemplate。例如:

<DataGridTemplateColumn Header="Pick a Date">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding myDate}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <DatePicker SelectedDate="{Binding myDate}" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>