Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据发送到非父窗口_C#_Wpf_Forms_Xaml_Scope - Fatal编程技术网

C# 将数据发送到非父窗口

C# 将数据发送到非父窗口,c#,wpf,forms,xaml,scope,C#,Wpf,Forms,Xaml,Scope,我有两个分部类,窗口A(frmSchedule)和窗口B(frmadlesson)。窗口A中有一个数据绑定的ListView控件。窗口A打开窗口B,该窗口设计用于创建新的课程对象,我想将该课程数据放回窗口A。我可以通过哪些方法来实现这一点?在C#中是否有一种简单的方法来使用应用程序范围变量 我曾尝试从一个基类派生两个分部类,并使用该类将课程数据传回第一个窗口,但我无法理解:( 有关更多信息,我在这里列出了该计划: 我有一个主窗口(fmrSchedule),其中ListView控件绑定到Obser

我有两个分部类,窗口A(frmSchedule)和窗口B(frmadlesson)。窗口A中有一个数据绑定的ListView控件。窗口A打开窗口B,该窗口设计用于创建新的课程对象,我想将该课程数据放回窗口A。我可以通过哪些方法来实现这一点?在C#中是否有一种简单的方法来使用应用程序范围变量

我曾尝试从一个基类派生两个分部类,并使用该类将课程数据传回第一个窗口,但我无法理解:(

有关更多信息,我在这里列出了该计划:

我有一个主窗口(fmrSchedule),其中ListView控件绑定到ObservableCollection: (为了简单起见,我假设Lesson对象只有一条重要的数据)

课程类别:

public class Lesson
{
    public Lesson()
    {
        //Stuff for later
    }

    private int m_Time;

    public int Time { get { return m_Time; } set { m_Time = value; } }
}

我不知道我怎么没意识到你能做到…非常感谢!我不知道我怎么没意识到你能做到…非常感谢!
public partial class frmSchedule : Window
{        
    public frmSchedule()
    {
        InitializeComponent();

        //ListView sample data
        aLesson = new Lesson();
        aLesson.Time = 9;
        m_myLessons.Add(aLesson);
        lstLessons.ItemsSource = LessonList;
    }

    Lesson aLesson;

    private ObservableCollection<Lesson> m_myLessons = new ObservableCollection<Lesson>();
    public ObservableCollection<Lesson> LessonList { get { return m_myLessons; } }

    //Add Lesson
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        //New frmAddLesson window
        frmAddLesson addLesson = new frmAddLesson();
        addLesson.Show();
    }
public partial class frmAddLesson : Window
{
    public frmAddLesson(System.DateTime? DateTime)
    {
        InitializeComponent();

        dateTime = DateTime;
        radPrivate.IsChecked = true;
    }

    //DateTime from calendar selection
    private DateTime? dateTime;
    //Lesson object
    private Lesson theLesson;

    //ADD LESSON
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        //Create new Lesson object
        theLesson = new Lesson();

        //Set Lesson property
        theLesson.Time = (int)cmbTime.SelectedValue; //Time

        this.Close();
    }
}
public class Lesson
{
    public Lesson()
    {
        //Stuff for later
    }

    private int m_Time;

    public int Time { get { return m_Time; } set { m_Time = value; } }
}
public partial class frmSchedule : Window 
{       
   ...  
   private void btnAdd_Click(object sender, RoutedEventArgs e)  
   {  
    //New frmAddLesson window  
    frmAddLesson addLesson = new frmAddLesson(this);  
    addLesson.Show();  
   }  

   public void AddLesson(Lesson lesson)
   {
     ...
   }
}

public partial class frmAddLesson : Window    
{
  public frmAddLesson(frmSchedule schedule)      
  {      
    InitializeComponent();      

    this.schedule = schedule;

    ...

  }    
  frmSchedule schedule;

  //ADD LESSON    
  private void btnAdd_Click(object sender, RoutedEventArgs e)    
  {    
    //Create new Lesson object    
    var theLesson = new Lesson();    

    //Set Lesson property    
    theLesson.Time = (int)cmbTime.SelectedValue; //Time    

    schedule.AddLesson(theLesson);

    this.Close();    
  }    
}