Nirav's Blog

Friday, July 30, 2010

Wednesday, May 26, 2010

Tiles View and ListView Display of Items in Silverlight


Generally in developing business application with Silverlight 3 or 4 we require to display item listing. Normally we display items in a List Box control. There may be certain scenarios where you require switching the way items displayed in ListBox. If you have visited any shopping site, you will find that products are displayed in multiple views like GridView(TilesView) or ListView. For example have a look at this web page: http://shopping.indiatimes.com/Electronics/LCD-Plasma-TVs/LCDs-TVs/ctl/20376651/pc/960067/cat/960377

I have developed a similar kind of experience in Silverlight 3.
               This sample depicts the way to implement listing of items in ListBox control. Here, you will see that user can switch the views from GridView(TilesView) to ListsView and vice versa.
               Technically, when you heard of such requirement you may find a bit difficult. But the idea which used to implement this requirement is so simple. You just need to create DataTemplate and ItemsPanelTemplate for ListBox. These templates should be separate for each TilesView and ListView. In case of ListView the ItemsPanel diplaying the items will be StackPanel with vertical orientation and for TilesView it will be WrapPanel.
               In the sample application I have created templates as given below:
1.      Tiles View:
DataTemplate
<DataTemplate x:Key="TilesDataTemplate">
                  <local:TilesView Margin="5"/>
      DataTemplate>
ItemsPanelTemplate
                     <ItemsPanelTemplate x:Key="TilesItemsPanelTemplate">
                  <Toolkit:WrapPanel/>
      ItemsPanelTemplate>
2.      Tiles View:
DataTemplate
<DataTemplate x:Key="ListDataTemplate">
                  <local:ListsView Margin="5"/>      
      DataTemplate>
ItemsPanelTemplate
                              <ItemsPanelTemplate x:Key="ListItemsPanelTemplate">
                  <StackPanel/>
      ItemsPanelTemplate>



You need to add above four templates in Resources. In order to switch the views programmatically, I have two buttons on the Silverlight page. On click of each button I switch the DataTemplate and ItemsPanelTemplate of the list box control.



This is how ListBox will be added in xaml:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2" x:Name="ProductsList" Width="{Binding Value, ElementName=WidthSlider}" Height="{Binding Value, ElementName=HeightSlider}" ItemTemplate="{StaticResource TilesDataTemplate}" ItemsPanel="{StaticResource TilesItemsPanelTemplate}"/>

By default the ListBox will display TilesView. You need to switch the relevant template for ListBox control on Button Click event as show below :


This is how the application will look like with two different views:


    Download the sample application from here: http://cid-cd9cdbf38105503c.skydrive.live.com/self.aspx/Blog%20Posts/ProductViewerSample.zip

Tuesday, April 13, 2010

Silverlight Image Button Control

Hi Guys,
If you came across certain requirements where you need to use an ImageButton control, you will notice that there is no out of box image button available in Silverlight. Actually it is very simple to use out of box Button control as Image button control. You just need to modify the control template of Button control to make it an ImageButton control.
          When you assign an ImageBrush as background of a button, you will not be able to see the image as background of the button because there lies certain xaml elements in front of the image. To achive such look you need to tweak the control template of the button a little bit.

I am providing you the style which will help you to achive the desired Image Button look for button control.

If you edit the control template of a button, you will see a Rectangle(BackgroundGradient) and a Border(BackgroundAnimation) as shown below:
Make visibility property of both of these control to 'Collapsed'.

Now, set image as background of the button.




Similarly, you can use the similar idea for ToggleButton also.
In case of ToggleButton you may need to require to switch the background image based on whether ToggleButton's property 'IsChecked' is True or False.

Here is a sample which convers both of these scenarios.

http://cid-cd9cdbf38105503c.skydrive.live.com/self.aspx/Blog%20Posts/ButtonStyleDemo.zip

Friday, March 5, 2010

Best Place to Learn Basic Concepts of WPF and Silverlight

Guys, visit http://www.wpftutorial.net/ to have in depth understanding of all the basic concepts related to WPF and Silverlight.

Thursday, February 11, 2010

Expression Blend Tutorials

Hi guys,
I found very good tutorials published by a blogger named "Gavin Wignall" on his blog. It will be very useful to you if you are a beginner. Have a look at : http://www.silverlightbuzz.com/tutorials/

Some more essential learning videos at: http://dotnet.dzone.com/articles/expression-blend-essentials

http://victorgaudioso.wordpress.com/2010/02/27/new-silverlight-video-tutorial-the-blend-ide-%E2%80%93-part-4-the-objects-timeline-and-states-panel/