如何在WPF中的数据网格视图中添加行

问题描述:

我想从另一个窗口文本框向数据网格视图添加一行.
该怎么办?

如果有人可以帮助我,谢谢.

这是我的网格视图代码

I want to add a row into data grid view from another window text box.
How can to do it?

If anyone can help me, thanks.

This is my grid view code

<ListView  Name="ListView1" Margin="6,6,783,145">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Icon" Width="75" DisplayMemberBinding="{Binding Path=Icon}"/>
                                <GridViewColumn Header="Position" Width="75"/>
                                <GridViewColumn Header="Name" Width="75"/>
                                <GridViewColumn Header="Gender" Width="75"/>
                                <GridViewColumn Header="Status" Width="75"/>
                                <GridViewColumn Header="Client ID" Width="75"/>
                            </GridView>
                        </ListView.View>
                    </ListView>

为了向WPF中的列表视图添加行,您希望将ListViewItemsSource属性绑定到一个集合.

此外,您不仅可以从实际的ListView UI元素,而且还可以从其后的任何代码向该集合添加项目(2-Way绑定将确保您UI中的项目将被更新).

In order to add rows to a list view in WPF you''d rather want to bind the ItemsSource property of the ListView to a collection.

Further you will be able to add items to that collection not just from actual ListView UI element, but also from any code behind (2-Way binding will assure you that the items in the UI will get updated).

<ListView  Name="ListView1" ItemsSource="{Binding YourCollectionName}">
     <ListView.View>
         <GridView>
             <GridViewColumn Header="Icon" Width="75" DisplayMemberBinding="{Binding Path=Icon}"/>
                 <GridViewColumn Header="Position" Width="75"/>
                 <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
          </GridView>
     </ListView.View>
</ListView>



这样就可以轻松地将另一个窗口的文本框中的项目添加到ListView 中:您需要将集合对象传递到第二个窗口,然后手动向其添加项目.
WPF将在第一个窗口中更新UI.

有关详细信息,请检查以下内容: http://www.switchonthecode.com/tutorials/wpf -tutorial-using-the-listview-part-1 [ ^ ]
问候



Adding items from another window''s textbox into your ListView will be easily then: you will need to pass your collection object to the second window and manually add items to it.
WPF will take care in updating the UI on the first window.

For details check this: http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1[^]
Regards


在DataGrid中动态添加行
Add row in DataGrid dynamically