无法在wpf中的datagird中添加行 [英] can not add rows in datagird in wpf

查看:57
本文介绍了无法在wpf中的datagird中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.....
我想在WPF的datagrid中动态添加行
是吗?

Hi Everyone.....
I want to add rows dynamically in datagrid in WPF
Is it right ?

dataGrid1.Items.Add(object newitem);


因为在我的程序中实际上添加了行,但未显示值...表示空白行..仅增加了行数.


谢谢您.


because in my program actually rows added but values are not shown ...means blank rows..only number of lines are incremented.


Thank You

推荐答案

与其将新对象添加到".items"集合中,不如采用数据绑定方法.听起来,您实际上已经成功添加了该行,但是还没有告诉Datagrid它应如何显示该行中的数据(例如,使用数据模板).请查看本教程以获取更多信息: http://wpftutorial.net/DataGrid.html

如果不是这种情况,那么我们将需要查看一些代码,最好是XAML& C#.

希望对您有所帮助.
Rather than adding new objects to the ".items" collection you may be better off taking a databinding approach. By sounds of things you are in fact adding the row successfully, but haven''t told the datagrid how it should display the data in that row (using a data template, for example). Check out this tutorial for more info: http://wpftutorial.net/DataGrid.html

If this is not the case then we''ll need to see some code, preferably both XAML & C#.

Hope that helps.


好吧,这是一个完整的示例.请注意,我在表达要在XAML中显示哪些列以及如何显示.

C#位:
Okay, here''s a fully working example. Note that I''m expressing which columns I want to display, and how, in the XAML.

The C# bit:
FileInfo[] files = new DirectoryInfo(System.IO.Path.GetTempPath()).GetFiles();
foreach (FileInfo fi in files)
{
    if (fi != null)
    {
        dataGrid1.Items.Add(fi);
    }
}



XAML位:



The XAML bit:

<DataGrid x:Name="dataGrid1">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="DirectoryName">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DirectoryName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>



如果有帮助,请标记为答案.



Please mark as the answer if that helps.


这篇关于无法在wpf中的datagird中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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