2014-09-19 14 views
0

下面我有一個從數據庫中刪除記錄的方法。它在同一個對話框中時效果很好。問題是我想在另一個窗口,窗體對話框而不是DataGrid上使用此方法。一旦我移動它,它就不再起作用。如何從另一個類調用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); 
    } 
} 

回答

0

也許你可以像

public DataGrid ProjectData { set; get; }

你可以前的DataForm顯示設置中的DataForm添加屬性。

相關問題