我的程序中有一点问题。我正在尝试将项添加到我的数据表中,然后让它显示在我的数据网格上。我可以使用主页中的一个简单代码来完成它,但我想要一个外部页面将结果发送给我,但我无法找到我在这里做错了什么。
主窗口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
namespace Træner_Program
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Display();
}
public DataTable dt = new DataTable();
public DataTable dtBind = new DataTable();
public void Display()
{
//I create the data in memory
//usually the date is get from DataBase and cached in memory to enhance the performance
dt = new DataTable();
dt.Columns.Add("Øvelse");
dt.Columns.Add("Vægt");
dt.Columns.Add("Sæt");
dt.Columns.Add("Gentagelser");
dt.Columns.Add("Pause");
dtBind = dt.Copy();
Binding bind = new Binding();
bind.Source = dtBind;
this.dtgPlan.SetBinding(ListView.ItemsSourceProperty, bind);
}
public void AddRow(string Øvelse, string Vægt, string Sæt, string Gentagelser, string Pause)
{
DataRow dr = dt.NewRow();
dr["Øvelse"] = Øvelse;
dr["Vægt"] = Vægt;
dr["Sæt"] = Sæt;
dr["Gentagelser"] = Gentagelser;
dr["Pause"] = Pause;
dt.Rows.Add(dr);
dtBind = dt.Copy();
Binding bind = new Binding();
bind.Source = dtBind;
this.dtgPlan.SetBinding(ListView.ItemsSourceProperty, bind);
}
private void btnGem_Click(object sender, RoutedEventArgs e)
{
//this is the working command from mainpage
//AddRow("Bænkpres", "80", "3", "10", "50");
}
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
var tilføjØvelse = new Tilføj_Øvelse();
tilføjØvelse.Show();
}
}
}
添加商品
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Træner_Program
{
/// <summary>
/// Interaction logic for Tilføj_Øvelse.xaml
/// </summary>
public partial class Tilføj_Øvelse : Window
{
MainWindow mw = new MainWindow();
public Tilføj_Øvelse()
{
InitializeComponent();
}
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
mw.AddRow("Bænkpres", "80", "3", "10", "50");
Close();
}
private void btnAnuller_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
答案 0 :(得分:2)
这是因为您正在创建一个新的MainWindow,而不是使用调用子窗口的初始MainWindow。
一个简单的修复可能是将Parent MainWindow的引用传递给Child,如下所示:
更新孩子:
public partial class Tilføj_Øvelse : Window
{
MainWindow mw;
public Tilføj_Øvelse(MainWindow mainWindow)
{
InitializeComponent();
mw = mainWindow;
}
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
mw.AddRow("Bænkpres", "80", "3", "10", "50");
Close();
}
private void btnAnuller_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
然后更新对它的调用:
private void btnTilføj_Click(object sender, RoutedEventArgs e)
{
var tilføjØvelse = new Tilføj_Øvelse(this);
tilføjØvelse.Show();
}
尽管如此,最好的(也是推荐的)方法是使用MVVM模式并为这两个Windows创建共享的ViewModel。