可编辑的数据表格 - CanUserAddRows="True" 不起作用

6

我有下面这个数据网格:

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

然而,我无法向DataGrid添加新行。请告诉我是否需要提供任何其他代码。
更新:
屏幕1:这是窗口加载时的屏幕截图,带有硬编码属性值。现在我看到了空的新行。
屏幕2:在这里,我已经添加了具有Rambo和Russia值的数据到新行中。现在,无论我做什么(切换,单击另一个单元格),下一个新行都不会被添加。我认为它应该添加一行新数据。
屏幕3:这里新添加的行值已经消失了。这是因为我双击两个空单元格之间的细边框。现在这很奇怪。
屏幕4:现在当我点击Peter单元格时,先前输入的行数据回来了,但现在它被推到下面,并在其前插入了一个新的空行。这非常奇怪。
请问有人能帮我理解DataGrid的这种行为吗?

1
你确定在你的DataGrid底部看到了一个空行吗?实际上,如果你在这一行输入内容,那么新的一行会自动出现在DataGrid底部。你期望看到这种行为吗?如果我的问题显得很明显,我很抱歉,但有时候人们的期望并不明显。 - ouflak
是的@ouflak,那就是我期望的行为。但是我没有看到新行自动出现在DataGrid底部。 - Lucifer
1
你的ItemsSource是什么类型?类型T必须具有无参构造函数。我回到办公室后会试一下这个。 - ouflak
我错过了无参数构造函数。谢谢你指出来。不过我正在面临另一个关于DataGrid的奇怪问题。请参考我问题中的Update部分。 - Lucifer
你更新了吗?我还没有看到...可能是刷新不起作用了... - ouflak
@ouflak,我更新了问题。这是我方面的小延迟 ;) - Lucifer
3个回答

16
在我的情况下,首先确保您的 ItemSource 不使用无法向其中添加新项的 array,请改用可以添加 newItem 的 List
此外,SomeClass 应该有一个不带参数的 默认构造函数
List<SomeClass>();

public Class SomeClass
{       
  public SomeClass() { }
}

然后,新的空行将出现在DataGrid底部。

请参考这个回答。


6
我将在此作为答案发布,因为我需要发布代码示例,并且评论开始变得冗长(收到了邀请聊天消息)。
对于原始问题的答案是确保ItemsSource的类型T具有无参数构造函数。
尝试将此代码附加到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;
}

如果您实际上在使用此处理程序进行其他操作,或者打算使用它进行其他操作,您可以检查OriginalSource以查看它是否为边框,并在该条件下取消事件。


那确实解决了问题,但只是部分地。我仍然无法添加任何进一步的行。 - Lucifer
好的,现在你可以看到原始的空行。你可以编辑它。但是没有额外的行被自动创建了?这样对吗? - ouflak
有人能够重现这个问题吗?还是我做错了什么? - Lucifer
我已经复制了它,但是我现在有点忙,而且我真的不知道该怎么做。我有一些笨拙的想法,比如处理单元格的选定事件并将新的“空白”项添加到ItemsSource中,或者类似这样的东西。但像那样的东西只是为了测试。内置功能应该可以正常工作。 耸肩 - ouflak

3
使用DataGridTextColumn和DataGridComboBoxColumn代替DataGridTemplateColumn,这样行就会被适当地添加。
如果你想使用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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接