将数据发送到非父窗口

本文关键字:窗口 数据 | 更新日期: 2023-09-27 18:27:10

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

我曾尝试从一个基类派生两个分部类,并使用该类将Lesson数据集中到第1个窗口,但我无法解决。:(

要了解更多信息,我在这里列出了程序:

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

    <ListView Name="lstLessons" Margin="204,15,192,125" ItemsSource="{Binding Path=LessonList}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Time}">Time</GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

代码中:

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();
    }

btAddd按钮控件打开第二个窗体(frmAddLesson),用于创建一个新的Lesson对象,添加到主窗口中的Lesson列表中:(在这种情况下,时间是根据组合框选择设置的)

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();    
  }    
}