Hi Guys,
I'm creating a WPF application, allowing a user to enter in details using CRUD operation using Entity Framework (also using a Repository pattern).
So far, I've created an Application that allows a user to enter a 'Prospect' client. I am able to edit this 'Prospect, add a new one and able to delete with ease.
This 'Prospect' though has a Foreign Key relation within another table known as 'ProspectMeeting', taking the ProspectID from the 'Prospect' table.
When I 'Select' a 'Prospect' from the ListView though, it doesn't show the data of that selected 'Prospect'. Further more, when I select a 'Prospect' from the ListView, the command should be enabled for me to add a 'ProspectMeeting' but when I do add, it crashes and produces this error;
Unable to cast object of type '*ProspectListViewModel' to type '*.ProspectViewModel'.
This is the code that it throws the error at (this is in the ProspectMeetingView);
private void btnAddMeeting_Click(object sender, RoutedEventArgs e) { ProspectMeetingView view = new ProspectMeetingView(); ProspectMeetingViewModel meeting = new ProspectMeetingViewModel();
//Throws the error on this line here// meeting.Prospect = (ProspectViewModel)this.DataContext; meeting.Mode = Mode.Add; view.DataContext = meeting; view.ShowDialog(); }
The Image below is the image of what I want to achieve - So hope that gives a better understanding of what I want to achieve.
Image may be NSFW.
Clik here to view.
In my repository I use this to get a List of 'ProspectMeetings' using the ProspectID;
public List<ProspectMeeting> GetMeetingByProspect(int _ProspectID) { using (var context = new DBEntities()) { return context.Prospects.First(i => i.ProspectID == _ProspectID).ProspectMeetings.ToList(); } }
In my ProspectViewModel, I use this Observable Collection to get the data from the 'ProspectMeetingViewModel;
private ObservableCollection<ProspectMeetingViewModel> prospectMeetings = null; public ObservableCollection<ProspectMeetingViewModel> ProspectMeetings { get { return GetProspectMeetings(); } set { prospectMeetings = value; OnPropertyChanged("ProspectMeetings"); } } internal ObservableCollection<ProspectMeetingViewModel> GetProspectMeetings() { ProspectMeetingRepository c = new ProspectMeetingRepository(); prospectMeetings = new ObservableCollection<ProspectMeetingViewModel>(); prospectMeetings.Clear(); foreach (DataObjects.ProspectMeeting i in c.GetMeetingByProspect(this.ProspectID)) { ProspectMeetingViewModel prospectMeeting = new ProspectMeetingViewModel(i); prospectMeeting.Prospect = this; prospectMeetings.Add(prospectMeeting); } return prospectMeetings; }
And in my ProspectListViewModel;
private ProspectListViewModel() : base("") { this.ProspectList = GetProspects(); } private ProspectViewModel selectedProspect = null; public ProspectViewModel SelectedProspect { get { return selectedProspect; } set { selectedProspect = value; OnPropertyChanged("SelectedProspect"); } } private ObservableCollection<ProspectViewModel> prospectList = null; public ObservableCollection<ProspectViewModel> ProspectList { get { return GetProspects(); } set { prospectList = value; OnPropertyChanged("ProspectList"); } } internal ObservableCollection<ProspectViewModel> GetProspects() { if (prospectList == null) prospectList = new ObservableCollection<ProspectViewModel>(); prospectList.Clear(); foreach (DataObjects.Prospect i in new ProspectRepository().GetAll()) { ProspectViewModel c = new ProspectViewModel(i); prospectList.Add(c); } return prospectList; }
I've been following this link to help me but I've become unstuck. Personally, When i look through my code, I think that it has something to do with the Repository not getting the data. But I am not too sure.
I hope some one could advice or me or help me with this issue. If more code needs to be added then just let me know, I only added the data that I thought was appropriate.
Many thanks,
Greg.