Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Sunday, May 23, 2010

Explaining Generics using WPF Example

1. Go to file, select new project and you will see a window like this:




2. Now select WPF Application and give the project name as "ShoppingExample" and then click on OK.

3. Now double-click on Window1.xaml and it will open a designer window for you. Go ahead and drop controls on this window and make it look like this:


4. Once you are done, just check out the XAML CODE, if you are familiar with asp.net, you will be so happy to see this. Everything you do here has a feel of developing web application.

5. If you are unable to find the xaml view of window1 then dont worry, i will help you find it.





6.Right click on the project in the solution explorer, go to Add-->Add New Item-->Select a class in the window and name it as ShoppingCartItem.cs.

7. We are going to do a simple shopping example where in the store manager can keep track of all the prices of items in his store.

8. So we every Item has a name and a price.
Lets write a class for ShoppingCartItem which will have two attributes called Name and Price.

namespace ShoppingExample
{
public class ShoppingCartItem
{
public string ItemName;
public double Price;

public ShoppingCartItem(string _Name,double _Price)
{
ItemName = _Name;
Price = _Price;

}

public override string ToString()
{
return ItemName+" Price is " + Price.ToString() + " Rupees Only";
}
}
}

9. This class has one constructor which initializes two variables ItemName and Price.The other method has been used to override the ToString() function from base class for our custom use.

Once you are done with this, you are almost there.

10. Now we are going to create a generics List to store a list of ShoppingCartItems Objects like this:

public List ItemsList = new List();

10. ItemsList will store list of Items like Item1,Item2,Item3.....etc which will has a (Name1,Price1),(Name2,Price2),.....etc.

11.Now Double Click on Window1.xaml.cs and open the file. In that file write the below method:-

private void Window_Loaded(object sender, RoutedEventArgs e)
{

lbItems.ItemsSource = ItemsList;

//lbItems is the ListBox Control dropped in window1 designer

}

12. This suggests that everytime the particular window loads, listbox control will be binded to ItemsList which we created before.

13. Allright everything is set, incase you are still confused about what are really to acheive then let me explain it to you.
This screen allows the user to enter the item name and price .User clicks on Add to cart and the item he entered in textboxes gets saved in the listbox below.
So to do this, we need to add first create a ShoppingCartItem and then add it to the Generic List ItemsList.Since the listbox is binded to our ItemsList, we just need to refresh the listbox everytime and item is added. Below is complete code for Windows1.xaml.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace ShoppingExample
{
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window
{
public List ItemsList = new List();
public Window1()
{
InitializeComponent();
}



private void btnAdd_Click(object sender, RoutedEventArgs e)
{
ShoppingCartItem I = new ShoppingCartItem(txtName.Text, Convert.ToDouble(txtPrice.Text));
ItemsList.Add(I);
lbItems.Items.Refresh();
txtName.Clear();
txtPrice.Clear();


}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

lbItems.ItemsSource = ItemsList;

}
}
}


10.Run the application(Press F5)











Free MCTS EXAM training tutorials on this website, soon will be launching video tutorials for all the same topics

No comments:

Post a Comment