WPF DataGrid无法在数据源为空时添加一行 [英] WPF DataGrid cannot Add a row when datasource is empty

查看:627
本文介绍了WPF DataGrid无法在数据源为空时添加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CanUserAddRows =True只有在 ItemsSource 中已有数据 DataGrid 。如果只是在原始的项目列表中没有行,那么 DataGrid 不显示占位符行输入新项目,即使我设置了 CanUserAddRows =True。为什么?

CanUserAddRows="True" only 'works' when there's already data in the ItemsSource of the DataGrid. If it just so happens that there are no rows in the original list of items, then the DataGrid doesn't display a placeholder row for entering new items, even though I've set CanUserAddRows="True". Why?!

提前感谢
Trindaz

Thanks in advance, Trindaz

推荐答案

p>这似乎是WPF DataGrid的一个已知问题。参见讨论这里(从第4条评论开始)也似乎在.net4中修复了。我在3.5和4(beta2)框架上对这个问题做了一些测试。请参阅以下结果:

This seem to be a known issue with WPF DataGrid. See discussion here (starting from the 4th comment) Also it seem to be fixed in .net 4. I've made some tests for this issue on 3.5 and 4 (beta2) frameworks. Pls, see results below:

首先我定义了3种类型的项目集合:

First I defined 3 types of item collections:

public class TestGridItems0 : ArrayList
{
}

public class TestGridItems1 : List<TestGridItem>
{
}

public class TestGridItems2<T> : List<TestGridItem>
{
}

其中TestGridItem如下:

where TestGridItem is below:

public class TestGridItem
{
    public string One { get; set; }
    public string Two { get; set; }
    public string Three { get; set; }
}

.net 3.5

TestGridItems0和TestGridItems1没有显示空集合的空行;其中TestGridItems2的工作正常。

TestGridItems0 and TestGridItems1 didn't show an empty line for an empty collection; where as TestGridItems2 did work fine.

.net 4

只有TestGridItems0 didn显示空集合的线条;其他2工作正常。

only TestGridItems0 didn't show the line for the empty collection; other 2 worked fine.

网格的xaml:

<my:DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="True">
    <my:DataGrid.Columns>
        <my:DataGridTextColumn Binding="{Binding One}" Header="One" />
        <my:DataGridTextColumn Binding="{Binding Two}" Header="Two" />
        <my:DataGridTextColumn Binding="{Binding Three}" Header="Three" />
    </my:DataGrid.Columns>
</my:DataGrid>

下面是如何分配项目来源:

below is how items source was assigned:

dataGrid.ItemsSource = new TestGridItems0();
dataGrid.ItemsSource = new TestGridItems1();
dataGrid.ItemsSource = new TestGridItems2<TestGridItem>();

希望这有助于,

这篇关于WPF DataGrid无法在数据源为空时添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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