473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to bind data into a WPF grid using 4 objects and MVVM pattern ?

49
Below i attached a sample Wpf. This is an Interview question.Can u people please help me with this ?It's very urgent.



Thanks for ur help,
Nagesh.
Attached Images
File Type: jpg Sample WPF.jpg (37.7 KB, 971 views)
Feb 8 '11 #1
3 8456
Frinavale
9,735 Expert Mod 8TB
You would create a View-Model Object that your XAML would bind to.

This View Model would contain private members of each class (model) that you want to display. It would wrap the properties of each private-class-member that needs to be displayed (that way the XAML can easily bind to the properties).

In this example your Model classes would be:
  1. Class
  2. Student
  3. Subject
  4. Address

Your View-Model class would have a private member of each of these classes:
Expand|Select|Wrap|Line Numbers
  1. Public Class StudentViewModel
  2.   Private _class As [Class]
  3.   Private _student As Student
  4.   Private _subject As Subject
  5.   Private _addresses As ObservableCollection(Of Address)
  6.  
  7.   'The constructor for the Student View Model class
  8.   Public Sub New(ByVal class As [Class], ByVal student As Student, ByVal address As Address, ByVal subject As Subject)
  9.     _class = class
  10.     _student = student
  11.     _addresses = ObservableCollection(Of Address)
  12.     _subject = Subject
  13. End Class
Now, you need to implement properties that expose the Model-class properties:

Expand|Select|Wrap|Line Numbers
  1. Public Class StudentViewModel
  2.   Private _class As [Class]
  3.   Private _student As Student
  4.   Private _subject As Subject
  5.   Private _addresses As ObservableCollection(Of Address)
  6.  
  7.   Public Property StudentName As String
  8.     Get
  9.       Return _student.Name
  10.     End Get
  11.     Set(ByVal value As String)
  12.       _student.Name = value
  13.     End Set
  14.   End Property
  15.   Public ReadOnly Property Addresses As ObservableCollection(Of Address)
  16.     Get
  17.       Return _addresses
  18.     End Get
  19.   End Property
  20.     Public Property SubjectName As String
  21.     Get
  22.       Return _subject.Name
  23.     End Get
  24.     Set(ByVal value As String)
  25.       _subject.Name = value
  26.     End Set
  27.   End Property
  28.  
  29.   '.......and so on'
  30.  
  31.   'The constructor for the Student View Model class
  32.   Public Sub New(ByVal class As [Class], ByVal student As Student, ByVal addresses As List(Of Address), ByVal subject As Subject)
  33.     _class = class
  34.     _student = student
  35.     _subject = Subject
  36.     _addresses New ObservableCollection(Of Address)(addresses)
  37. End Class
Now in your XAML you can bind objects to the Properties of your View Model to display/interact with the data.

-Frinny
Feb 9 '11 #2
nagmvs
49
Thanks for ur reply Frinny.
Feb 11 '11 #3
Thanks Frinavale its very informative and useful for me.your example is very helpful
Nov 8 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: C | last post by:
I am creating a Data Grid using the Property Builder and I want a link to pass two variables in a Hyperlink Column. How can I accomplish this? How can I add two data columns under the HyperLink...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
3
by: Snake | last post by:
I have a vb .net program which fills a data grid upon form load from an acccess database. This works great. Now, I have to add a combo box and use it to alter the underlying sql statement and...
1
by: josephrthomas | last post by:
hi...can someone pls show me how can i connect asp.net to access and retrieve data from it and show it in a datagrid pls??? the datagrid has all the lines needed to put the data from the access...
1
by: Joe Dunleavy | last post by:
Hi Guys, Hopefully some one will have come across this issue already. I have a DataGrid inside a Div where a scroll bar displays once a certain height is meet --> <div id="dvSales"...
0
by: hlam | last post by:
Help - Calculating the total of a column in a data grid -- when data grid is part of Master-Detail set-up I have setup a Master-Detail form using Visual Studio.Net. A ListBox is the (Master)...
3
by: vineetbatta | last post by:
I have Custom Data class which stores data about single customer and then i store that customer objects in arraylist as shown below. Customer custdata = null; // Custom Data class for 1 customer...
16
aspamit
by: aspamit | last post by:
Hi all, I am displaying data in data-grid using dataset.Now I want to select row of data-grid and fetch the values into it in text-boxes on the same page. I mean I want to select the row i.e, by...
4
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi All, I have an application that fetch data thru a store proc and display in a datagrid, but after successful execution of proc the data grid is not visible.Please go thru the following code...
2
by: livmacca | last post by:
Hi, I am trying to create a web page whereby I first read data from Oracle Database and display the records out on the data grid. The data displayed is in order of one attribute say 'A' meaning...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.