I have an object similar to
class Quote
{
string Ticker {set; get; }
string Rating {set; get;}
People Sender {set; get;}
}
Peaple is defined as,
class People
{
string Email {set; get;}
int Age {set; get;}
string Firm {set; get;}
}
I also have an ObservableCollection of Quote,
ObservableCollection <Quote> QuoteList;
Now I would like to display the QuoteList in a DataGrid, such that each row have the following 3 cells,
1. Quote.Ticker
2. Quote.Rating
3. Quote.Sender.Email
I also would like to, if I click each row, I can get the Quote object of that clicked row.
May I ask how can I achieve that?
If you bind your
DataGridto anItemsSourceof anObservableCollection<Quote>eachDataGridRowwill be built with theQuoteobject as it’sDataContext(data layer).By default, a DataGrid will automatically generate a column for each property on the data item, so in your case it would generate a column for
Ticker,Rating, andSender.You can either override that by setting
AutoGenerateColumns="False"and defining your own<DataGrid.Columns>, which includes a column bound toSender.Emailor you can use an implicit DataTemplate to tell WPF to draw the
Senderobject with a string containing theEmailpropertyOf the two, I’d pick the first one for your situation since you won’t need to code anything extra for things like editing
As for getting the data object when the row is clicked, simply check out the
DataContextof the row, or bind yourCommandParameterto"{Binding }"