Skip to content

@userXemY /MainViewModel.cs secret
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
SelectedItem of a Datagrid bound to another Datagrid.
<!--app.config file-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=User476;Initial Catalog=Person;Persist Security Info=True;User ID=Dbg;Password=x7H8U"/>
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tryout13022013.PersonViewModels;
using System.ComponentModel;
using tryout13022013.DetailsViewModel;
using tryout13022013.PersonModel;
namespace tryout13022013
{
public class MainViewModel
{
private PersonViewModel _subPerson = new PersonViewModel();
public PersonViewModel SubPerson
{
get
{
return _subPerson;
}
set
{
if (_subPerson != value)
{
_subPerson = value; OnPropertyChanged("SubPerson");
}
}
}
private PersonDetailsViewModel _subDetail = new PersonDetailsViewModel();
public PersonDetailsViewModel SubDetail
{
get { return _subDetail; }
set
{
_subDetail = value; OnPropertyChanged("SubDetail");
}
}
private Person _selectedPerson;
public Person SelectedPerson
{
get { return _selectedPerson; }
set {
if (_selectedPerson != value)
{
_selectedPerson = value;
OnPropertyChanged("SelectedPerson");
if (this.SelectedPerson != null && this.SubDetail != null)
{
// I dont know how to call the PersonDetailsViewModel class like a method here in order to load its data. kindly help
this.SubDetail.MethodforLoadingPersonDetails(this.SelectedPerson);
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
}
<Window x:Class="tryout13022013.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:person="clr-namespace:tryout13022013.PersonViewModels"
xmlns:details="clr-namespace:tryout13022013.DetailsViewModel"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding SubPerson}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}"
AutoGenerateColumns="False" Height="77" HorizontalAlignment="Left"
Margin="101,26,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="auto" >
<DataGrid.DataContext>
<person:PersonViewModel/>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="auto" Binding="{Binding PersonID}"/>
<DataGridTextColumn Header="First Name" Width="auto" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Width="auto" Binding="{Binding LastName}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding SubDetail}"
AutoGenerateColumns="False" Height="77" HorizontalAlignment="Left"
Margin="101,135,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="255" >
<DataGrid.DataContext>
<details:PersonDetailsViewModel/>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="auto" Binding="{Binding PersonID}"/>
<DataGridTextColumn Header="Address" Width="auto" Binding="{Binding Address}"/>
<DataGridTextColumn Header="Position" Width="auto" Binding="{Binding Description}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
namespace tryout13022013.PersonModel
{
public class Person : INotifyPropertyChanged
{
private int _PersonID;
private string _FirstName;
private string _LastName;
public int PersonID
{
get { return _PersonID; }
set
{
_PersonID = value;
RaisePropertyChanged(this, "Title");
}
}
public string FirstName
{
get { return _FirstName; }
set
{
_FirstName = value;
RaisePropertyChanged(this, "FirstName");
}
}
public string LastName
{
get { return _LastName; }
set
{
_LastName = value;
RaisePropertyChanged(this, "LastName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(object sender, string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
namespace tryout13022013.DetailsModel
{
public class PersonDetails : INotifyPropertyChanged
{
private int _PersonID;
public int PersonID
{
get { return _PersonID; }
set
{
_PersonID = value;
RaisePropertyChanged(this, "Title");
}
}
private string address;
public string Address
{
get { return address; }
set
{
address = value;
RaisePropertyChanged(this, "Address");
}
}
private string _Description;
public string Description
{
get { return _Description; }
set
{
_Description = value;
RaisePropertyChanged(this, "Description");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(object sender, string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.EntityModel;
using tryout13022013.DetailsModel;
using System.Windows.Threading;
using tryout13022013.DetailsModel;
namespace tryout13022013.DetailsViewModel
{
public class PersonDetailsViewModel : INotifyPropertyChanged
{
private bool disposed;
private DispatcherTimer timer = new DispatcherTimer();
Model _myModel = new Model();
private ObservableCollection<PersonDetails> _person = new ObservableCollection<PersonDetails>();
public ObservableCollection<PersonDetails> DetailsData
{
get { return _person; }
set
{
_person = value;
OnPropertyChanged("DetailsData");
}
}
public PersonDetailsViewModel()
{
initializeload();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 3);
timer.Start();
}
~PersonDetailsViewModel()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
timer.Stop();
timer.Tick -= new EventHandler(timer_Tick);
}
disposed = true;
}
}
private void timer_Tick(object sender, EventArgs e)
{
try
{
DetailsData.Clear();
initializeload();
}
catch (Exception ex)
{
timer.Stop();
Console.WriteLine(ex.Message);
}
}
private void initializeload()
{
try
{
DataTable table = _myModel.getData();
for (int i = 0; i < table.Rows.Count; ++i)
DetailsData.Add(new PersonDetails
{
PersonID = Convert.ToInt32(table.Rows[i][0]),
Address = table.Rows[i][1].ToString(),
Description = 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].[personDetails]", sqlcon);
da.Fill(ndt);
da.Dispose();
sqlcon.Close();
return ndt;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data.EntityModel;
using System.Windows.Threading;
using tryout13022013.PersonModel;
namespace tryout13022013.PersonViewModels
{
public class PersonViewModel : INotifyPropertyChanged
{
private bool disposed;
private DispatcherTimer timer = new DispatcherTimer();
Model _myModel = new Model();
private ObservableCollection<Person> _person = new ObservableCollection<Person>();
public ObservableCollection<Person> PersonData
{
get { return _person; }
set
{
_person = value;
OnPropertyChanged("PersonData");
}
}
public PersonViewModel()
{
initializeload();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 3);
timer.Start();
}
~PersonViewModel()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
timer.Stop();
timer.Tick -= new EventHandler(timer_Tick);
}
disposed = true;
}
}
private void timer_Tick(object sender, EventArgs e)
{
try
{
PersonData.Clear();
initializeload();
}
catch (Exception ex)
{
timer.Stop();
Console.WriteLine(ex.Message);
}
}
private void initializeload()
{
try
{
DataTable table = _myModel.getData();
for (int i = 0; i < table.Rows.Count; ++i)
PersonData.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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.