here is the code so far I have tried
Baseclass.cs
public class Baseclass { public event PropertyChangedEventHandler PropertyChanged; protected void SetProperty<T>(ref T member, T value, [CallerMemberName] string propertyName = null) { member = value; this.RaiseNotification(propertyName); } protected void RaiseNotification(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }
person.cspublic class person : Baseclass { private int personID; public int PersonID { get { return personID; } set { this.SetProperty(ref this.personID, value); } } private string firstName; public string FirstName { get { return firstName; } set { this.SetProperty (ref this.firstName, value); } } private string lastName; public string LastName { get { return lastName; } set { this.SetProperty (ref this.lastName, value); } } Model _personModel = new Model(); private ObservableCollection _person = new ObservableCollection (); public ObservableCollection Getpersons { get { return _person; } set { _person = value; OnPropertyChanged("GetPersons"); } } public person() { initializeload(); } private void initializeload() { try { DataTable table = _personModel.getData(); for (int i = 0; i < table.Rows.Count; ++i) Getpersons.Add(new person { PersonID = Convert.ToInt32(table.Rows[i][0]), FirstName = table.Rows[i][1].ToString(), LastName = table.Rows[i][2].ToString(), }); } catch (Exception e) { Console.WriteLine(e.Message); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyname) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyname)); } public class Model { public DataTable getData() { DataTable ndt = new DataTable(); SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); sqlcon.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Person].[dbo].[persons]", sqlcon); da.Fill(ndt); da.Dispose(); sqlcon.Close(); return ndt; } } } PersonDetail.cspublic class PersonDetails : Baseclass { private int personID; public int PersonID { get { return personID; } set { this.SetProperty(ref this.personID, value); } } private string address; public string Address { get { return address; } set { this.SetProperty (ref this.address, value); } } private string pos; public string Position { get { return pos; } set { this.SetProperty (ref this.pos, value); } } DetailsModel _detailModel = new DetailsModel(); private ObservableCollection<PersonDetails> _details = new ObservableCollection<PersonDetails>(); public ObservableCollection<PersonDetails> GetDetails { get { return _details; } set { _details = value; OnPropertyChanged("GetDetails"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyname) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyname)); } public PersonDetails() { initializeload(); } private void initializeload() { try { DataTable table = _detailModel.getData(); for (int i = 0; i < table.Rows.Count; ++i) GetDetails.Add(new PersonDetails { PersonID = Convert.ToInt32(table.Rows[i][0]), Address = table.Rows[i][1].ToString(), Position = table.Rows[i][2].ToString(), }); } catch (Exception e) { Console.WriteLine(e.Message); } } public class DetailsModel { public DataTable getData() { DataTable ndt = new DataTable(); SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); sqlcon.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [Person].[dbo].[personDetails]", sqlcon); da.Fill(ndt); da.Dispose(); sqlcon.Close(); return ndt; } } } XAMLMargin="100,20,116,211" ItemsSource="{Binding person}" SelectedItem="{Binding Selectedperson}" /> Margin="100,130,116,101" ItemsSource="{Binding personDetails}" /> can anybody help to proceed in writing the MainViewModel? I am stuck here since weeks.
DharshiniPosted Feb 12, 2013, 4:26 AM
Santhosh Kumar JayaramanPosted Feb 12, 2013, 12:12 AM
This will become an infinite loop because everytime in initialize load it get datatable and for each record in datatable you are trying to create new person and that will again call initialize load. Please modify it