如何调用DataGrid';s”;所选项目“;来自另一个班级
本文关键字:项目 选项 另一个 何调用 调用 DataGrid | 更新日期: 2023-09-27 18:22:22
下面我有一个从数据库中删除记录的方法。当它在同一个对话框中时,效果很好。问题是,我希望在另一个窗口,窗体对话框,而不是DataGrid上使用此方法。一旦我移动它,它就不再工作了。
public partial class ProjectsTable : Window
{
public ProjectsTable()
{
InitializeComponent();
}
//populates a DataGrid called "ProjectData" on the "ProjectsTable.xaml"
private void Window_Loaded(Object sender, RoutedEventArgs e)
{
BillableProjectsDataContext project = new BillableProjectsDataContext();
List<Project> projects = (from p in project.Projects
select p).ToList();
ProjectData.ItemsSource = projects;
}
// deletes the selected project
private void btnDeleteProject_Click(object sender, RoutedEventArgs e)
{
//this is the most important part, it tells the method which record to delete
Project selected = ProjectData.SelectedItem as Project;
//calls another class called "Menu_SQL" and uses it's DeleteProject method
Menu_SQL.DeleteProject(selected);
Window_Loaded(null, null);
}
}
上面的方法很好,问题是我想在我的窗体上使用这个方法,这是另一个窗口一旦我移动它,它就不再工作了。
特定错误为"名称"ProjectData"在当前上下文中不存在"。
但它确实存在,它是DataGrid的名称。
public partial class DataForm : Window
{
public Project project;
public DataForm(Project project)
{
InitializeComponent();
this.project = project;
}
public DataForm()
{
InitializeComponent();
}
// deletes the selected project, the same exact method as before, just in another window
private void btnDeleteProject_Click(object sender, RoutedEventArgs e)
{
//this is the most important part, it tells the method which record to delete
// the problem is that this line will error because "The name 'ProjectData' does not exist in the current context"
Project selected = ProjectData.SelectedItem as Project;
Menu_SQL.DeleteProject(selected);
}
}
也许你可以在DataForm中添加一个属性,比如
public DataGrid ProjectData { set; get; }
您可以在DataForm显示之前设置它。