1
我有2個部分類,窗口A(frmSchedule)和窗口B(frmAddLesson)。窗口A中有一個數據綁定的ListView控件。窗口A打開窗口B,該窗口旨在創建一個新的課程對象,並且我想將該課程數據放回到窗口A.我可以通過哪些方式實現此目標?有沒有簡單的方法在C#中使用應用程序範圍變量?發送數據到非父窗口
我試過從單個基類派生兩個部分類,然後使用該類將課程數據導回第一個窗口,但我無法弄清楚。 :(
欲瞭解更多信息,我已經奠定了這裏的程序:
我有一個主窗口(fmrSchedule)綁定到一個ObservableCollection ListView控件: (爲簡單起見,我會假裝課程對象僅具有1個數據的該事項)
<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();
}
的btnAdd按鈕控制打開第二形式(frmAddLesson),其用於創建一個新的課程對象被添加到該課程列表在主窗口:(在此情況下,時間是基於一個組合框選擇集)
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; } }
}
我不知道我怎麼不知道,你可以做到這一點...非常感謝! – 2012-03-07 04:33:48