I have 2 partial classes, Window A(frmSchedule) and Window B(frmAddLesson). Window A has a data-bound ListView control in it. Window A opens up Window B, which is designed to create a new Lesson object, and I want to put that lesson data back into Window A. What are some of the ways I can accomplish this? Is there an easy way to use application-scope variables in C#?
I’ve tried deriving both partial classes from a single base class and using that class to funnel the Lesson data back to the 1st window, but I can’t figure it out. 🙁
For more info, I’ve laid out the program here:
I have a main window(fmrSchedule) with a ListView control bound to an ObservableCollection:
(For the sake of simplicity, I’ll pretend the Lesson object has only 1 piece of data that matters)
<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>
In the code:
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();
}
The btnAdd button control opens a 2nd form(frmAddLesson), which serves to create a new Lesson object to be added to the Lesson list in the main window: (in this case, time is set based on a combobox selection)
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();
}
}
The Lesson class:
public class Lesson
{
public Lesson()
{
//Stuff for later
}
private int m_Time;
public int Time { get { return m_Time; } set { m_Time = value; } }
}
1 Answer