从另一个窗口更新WPF中的DataGrid [英] Update DataGrid in WPF from another window

查看:207
本文介绍了从另一个窗口更新WPF中的DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为MainWindow的窗口上有一个DataGrid控件。从MainWindow我打开另一个名为NewCustomerWindow的窗口,使用以下代码点击按钮。

  NewCustomerWindow newCustomerWindow = new NewCustomerWindow(); 
newCustomerWindow.ShowDialog();

在NewCustomerWindow中,我有一个带有文本框的表单,有人可以输入有关客户的信息,然后是这些信息被保存到数据库中。在我保存这些信息后,我想更新MainWindow中的DataGrid,所以我使用以下代码

  var mainWindow = Application.Current .Windows 
.OfType< MainWindow>()
.FirstOrDefault(window => window是MainWindow);

mainWindow.customersGrid.Items.Add(new
{
CustomerID = txtCustomerID.Text,
CompanyName = txtCompanyName.Text,
ContactName = txtContactName。文本,
ContactTitle = txtContactTitle.Text,
地址= txtAddress.Text,
City = txtCity.Text,
Region = txtRegion.Text,
PostalCode = txtPostalCode。文本,
国家= txtCountry.Text,
电话= txtPhone.Text,
传真= txtFax.Text
});

上述代码返回以下错误:在使用ItemsSource时,操作无效。使用ItemsControl.ItemsSource访问和修改元素。



customersGrid是MainWindow中DataGrid的名称,其结构如下。

 < DataGrid AutoGenerateColumns =FalseHorizo​​ntalAlignment =StretchMargin =8,29,8,8Name = customersGridVerticalAlignment =StretchLoaded =customersGrid_Loaded> 
< DataGrid.Columns>
< DataGridTextColumn Width =AutoBinding ={Binding CustomerID}Header =ΚωδικόςΠελάτη/>
< DataGridTextColumn Width =AutoBinding ={Binding CompanyName}Header =ΟνομαΕταιρίας/>
< DataGridTextColumn Width =AutoBinding ={Binding ContactName}Header =ΟνομαΕπαφής/>
< DataGridTextColumn Width =AutoBinding ={Binding ContactTitle}Header =ΤίτλοςΕπαφής/>
< DataGridTextColumn Width =AutoBinding ={Binding Address}Header =Διεύθυνση/>
< DataGridTextColumn Width =AutoBinding ={Binding City}Header =Πόλη/>
< DataGridTextColumn Width =AutoBinding ={Binding Region}Header =Περιοχή/>
< DataGridTextColumn Width =AutoBinding ={Binding PostalCode}Header =ΤαχυδρομικόςΚώδικας/>
< DataGridTextColumn Width =AutoBinding ={Binding Country}Header =Χώρα/>
< DataGridTextColumn Width =AutoBinding ={Binding Phone}Header =Τηλέφωνο/>
< DataGridTextColumn Width =AutoBinding ={Binding Fax}Header =Fax/>
< /DataGrid.Columns>
< / DataGrid>

我想一个解决方案是使用所有数据再次设置DataGrid的ItemsSource属性,但是不是一个好的选择,因为客户的数据来自一个wcf服务,带来了很多记录。那么这个问题有什么好的解决方案呢?谢谢你提前。

解决方案

你设置你的 MainWindow.DataContext = MainViewModel.Instance


将您的DataGrid绑定到客户

 < DataGrid ItemsSource ={Binding Customers}/>所以当你想更新datagrid你只是更新客户(例如从NewCustomerWindow)和dataGrid的

将自动更新



这里是很好的例子:



- 实用 - 快速启动 - MVVM-in-WPF中的教程 - / a>



DataGrid教程


I have a DataGrid control on a window called MainWindow. From MainWindow i open another window called NewCustomerWindow using the following code on a button click.

NewCustomerWindow newCustomerWindow = new NewCustomerWindow();
newCustomerWindow.ShowDialog();

In the NewCustomerWindow i have a form with textboxes where someone can enter information about a customer and then these information are saved into a database. After i save these information i want to update the DataGrid in the MainWindow so i use the following code

var mainWindow = Application.Current.Windows
                .OfType<MainWindow>()
                .FirstOrDefault(window => window is MainWindow);

mainWindow.customersGrid.Items.Add(new 
            {
                CustomerID = txtCustomerID.Text,
                CompanyName = txtCompanyName.Text,
                ContactName = txtContactName.Text,
                ContactTitle = txtContactTitle.Text,
                Address = txtAddress.Text,
                City = txtCity.Text,
                Region = txtRegion.Text,
                PostalCode = txtPostalCode.Text,
                Country = txtCountry.Text,
                Phone = txtPhone.Text,
                Fax = txtFax.Text
            });

The above code returns the following error: "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."

customersGrid is the name of the DataGrid in MainWindow and its structure is as follows.

<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="8,29,8,8" Name="customersGrid" VerticalAlignment="Stretch" Loaded="customersGrid_Loaded">
        <DataGrid.Columns>
            <DataGridTextColumn Width="Auto" Binding="{Binding CustomerID}" Header="Κωδικός Πελάτη"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding CompanyName}" Header="Όνομα Εταιρίας"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding ContactName}" Header="Όνομα Επαφής"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding ContactTitle}" Header="Τίτλος Επαφής"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding Address}" Header="Διεύθυνση"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding City}" Header="Πόλη"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding Region}" Header="Περιοχή"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding PostalCode}" Header="Ταχυδρομικός Κώδικας"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding Country}" Header="Χώρα"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding Phone}" Header="Τηλέφωνο"/>
            <DataGridTextColumn Width="Auto" Binding="{Binding Fax}" Header="Fax"/>
        </DataGrid.Columns>
    </DataGrid>

I suppose that a solution is to set the ItemsSource property of the DataGrid again with all the data but this is not a good choice because the customers data is coming from a wcf service which brings a lot of records. So, what is a good solution to this problem? Thank you in advance.

解决方案

you set your MainWindow.DataContext = MainViewModel.Instance (your choice to create it a singleton or not)

your MainViewModel class should have an observableCollection dependency property lets call it Customers. you bind your DataGrid to to Customers

<DataGrid ItemsSource="{Binding Customers}" />

so when you want to update the datagrid you just update Customers (for example from NewCustomerWindow) and the dataGrid will automatically be updated

here are nice examples:

A-Practical-Quick-start-Tutorial-on-MVVM-in-WPF

DataGrid Tutorial

这篇关于从另一个窗口更新WPF中的DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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