.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

CynotWhyNot Silverlight Gallery has 5 New Additions

I have been busy adding new demos to the Silverlight Gallery application and since it's introduction, there have been five new entries added.

  1. XAML From String Given a XAML fragment as a sting, create a XAML object.
  2. Video Player  Introduced in a previous post, this full featured video player, is easy to use. Just add one line of XAML and Bob's your uncle.
  3. Declarative Animations 1 Using only XAML this demo shows how to animate a simple rectangle. The example uses the Rectangle.Loaded event trigger.
  4. Declarative Animations 2 Again some animation using only XAML but this time we use the Visual State Manager.
  5. Databinding 2 Two way data binding to a collection of objects. The collection is bound to a data grid, combo box, and the new toolkit chart control. Check out the Toolkit, lots of great Silverlight controls

As always you can run the demos and download the complete source code. Just visit CynotWhyNot Silverlight Gallery.

Enjoy!

Guess the movie

Capricorn 15's. Born 2244. Enter the Carousel. This is the time of renewal.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Saturday, November 08, 2008 3:00 PM
Permalink | Comments (95) | Post RSSRSS comment feed

Fun with Silverlight and jQuery

Microsoft recently announced that it will be shipping jQuery with Visual Studio. This is remarkable announcement since they did not acquire jQuery and will not be creating there own version of jQuery but rather they will be including jQuery as is and will provide 24/7 support for the product. In addition MS will be making regular contributions to the jQuery library following the same rules as everyone else contributing to jQuery.

The jQuery site already has a link to where you can download the jquery-1.2.6-vsdoc.js. This file contains includes the XML comments that when included will provide rich intellisense for jQuery. That's right intellisense support for jQuery in VS2008. Download jQuery intellisense file from this site. Just select the Visual Studio link.

jQuery's core API is extremely powerful providing excellent selector capabilities, simplifying HTML documentation traversing. jQuery also simplifies event handling, animating and Ajax interactions. Lately, I've been playing around with jQuery Plugins and wow. There are currently 1000+ plugins available at the jQuery site.

As an exercise I thought it would fun to write a Silverlight application and incorporate some jQuery. I decided to use the Drag and Drop plugin called $.event.special.drag. How well would this  plugin work with Silverllight? Let's find out.

Implementing dragging using this plugin is simple, just create a div tag and then call the Javascript API passing the div tag. The following  shows how to implement dragging. I've embedded a Silverlight control within the div tag.  

    1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">

    2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">

    3 

    4   <head>

    5     <title>Drag with jQuery</title>

    6     <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    7     <meta http-equiv="imagetoolbar" content="no" />

    8 

    9     <style type="text/css" media="screen">

   10         body {

   11           font: 12px/16px Verdana, Arial, Helvetica, sans-serif;

   12           color: #002;

   13           padding: 0;

   14           margin: 12px;

   15         }

   16         .silverlight {

   17           position: absolute;

   18           left: 10px;

   19           top: 10px;

   20           height: 500px;

   21           width: 700px;

   22           background: #CCF;

   23           border: 1px solid #AAD;

   24           text-align: center;

   25           font-size: 14px;

   26         }

   27         .sloverlay {

   28           height: 500px;

   29           width: 700px;

   30           text-align: center;

   31           font-size: 14px;

   32           float: left;

   33         }

   34         .stuff {

   35           height: 100px;

   36           width: 200px;

   37           background: #777777;

   38           float: left;

   39           margin-left: 10px;

   40         }

   41         .title {

   42           cursor: move;

   43           background: #AAD;

   44           height: 20px;

   45         }

   46     </style>

   47 

   48     <script src="jquery.js" type="text/javascript"></script>

   49     <script src="jquery.event.drag.js" type="text/javascript"></script>

   50 

   51     <script type="text/javascript">

   52       //

   53       $(document).ready(function(){

   54           $('#silverlight')

   55             .bind('dragstart', function(event) {

   56             return $(event.target).is('.title');

   57           })

   58             .bind('drag', function(event) {

   59             $(this).css({

   60               top: event.offsetY,

   61               left: event.offsetX

   62             });

   63           });

   64       });

   65     </script>

   66 

   67   </head>

   68 

   69   <body>

   70     <div class="sloverlay"></div>

   71     <div class="stuff"></div>

   72     <div id="silverlight" class="silverlight">

   73       <div class="title">You can drag this entire window.</div>

   74         <object data="data:application/x-silverlight,"

   75           type="application/x-silverlight-2"

   76           width="100%" height="485px">

   77           <param name="source" value="ClientBin/DragAndDrop.xap"/>

   78           <param name="background" value="white" />

   79           <a href="http://go.microsoft.com/fwlink/?LinkID=115261"

   80             style="text-decoration: none;">

   81               <img src="http://go.microsoft.com/fwlink/?LinkId=108181"

   82               alt="Get Microsoft Silverlight"

   83               style="border-style: none"/>

   84           </a>

   85         </object>

   86         <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>

   87     </div>

   88   </body>

   89 

   90 </html>

You can check out this application in action by navigating to this site. Try dragging the window that contains the Silverlight control. Just hover over the title, press the left mouse button and move the window to a new location. This all worked without any complications. 

Guess the movie

I have no armour left. You've stripped it from me. Whatever is left of me - whatever is left of me - whatever I am - I'm yours.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Wednesday, November 05, 2008 10:17 AM
Permalink | Comments (86) | Post RSSRSS comment feed

Silverlight Data Binding, using INotifyPropertyChanged and ObservableCollection

Silverlight is a subset of WPF and it is no surprise that Silverlight's data binding model borrows from WPF. Both technologies use the INotifyPropertyChanged interface and ObservableCollection generic to easily incorporate two way data binding into an application. A CLR object that implements the INotifyPropertyChanged interface and raises a PropertyChanged event whenever a bindable property changes is easily added into the XAML using the binding syntax. Listing 1 shows how to create a class that implements INotifyPropertyChanged and raises the PropertyChanged event. Listing 2 shows how to bind the properties to Text boxes in XAML.

Listing 1: Implementing the INotifyPropertyChanged Interface

    1 Imports System.ComponentModel

    2 

    3 Public Class Person

    4     Implements INotifyPropertyChanged

    5 

    6     Public Event PropertyChanged(ByVal sender As Object, _

    7         ByVal e As System.ComponentModel.PropertyChangedEventArgs) _

    8         Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    9 

   10     Private Sub FirePropertyChanged(ByVal [property] As String)

   11         If Not String.IsNullOrEmpty([property]) Then

   12             RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs([property]))

   13         End If

   14     End Sub

   15 

   16     Private _Name As String

   17     Public Property Name() As String

   18         Get

   19             Return _Name

   20         End Get

   21         Set(ByVal value As String)

   22             _Name = value

   23             FirePropertyChanged("Name")

   24         End Set

   25     End Property

   26 

   27     Private _Count As Integer

   28     Public Property Count() As Integer

   29         Get

   30             Return _Count

   31         End Get

   32         Set(ByVal value As Integer)

   33             _Count = value

   34             FirePropertyChanged("Count")

   35         End Set

   36     End Property

   37 

   38     Public Sub New(ByVal name As String, ByVal count As Integer)

   39         Me.Name = name

   40         Me.Count = count

   41     End Sub

   42 

   43 End Class

Listing 2: XAML Data Binding syntax

    6         <TextBox Text="{Binding Name, Mode=TwoWay}" />

    7         <TextBox Text="{Binding Count, Mode=TwoWay}" />

All that is needed is to set the DataContext of a parent container to an instance of the Person object and that's it, you now have two way data binding. Whenever the underlying object is modified the UI will automatically be updated and whenever the UI is changed the object is updated. Now that's pretty simple.

Now what if we need to bind a collection to a data grid, combo box or even a chart? You could write a custom collection class that implements INotifyPropertyChanged or better yet you can use the ObservableCollection generic class, which already implements the INotifyPropertyChanged. Listing 3 uses the ObservableCollection generic to create a collection of the above Person class.

Listing 3: Using the ObservableCollection Generic

   13     Private members As New ObservableCollection(Of Person)

   14 

   15     Private Sub Page_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) _

   16         Handles Me.Loaded

   17         members.Add(New Person("Tony", 5))

   18         members.Add(New Person("Sally", 2))

   19         members.Add(New Person("John", 8))

   20         grid.ItemsSource = members

   21         comboBoxName.ItemsSource = members

   22         CType(chart.Series(0), DynamicSeriesWithAxes).ItemsSource = members

   23     End Sub

The above snippet also includes how to bind the collection to a data grid, chart and combo box. The XAML used to bind this collection to the previously mentioned controls is shown in Listing 4.

Listing 4: Xaml Binding syntax for Datagrid, Chart and ComboBox

   17         <data:DataGrid x:Name="grid" Grid.Row="1" Grid.Column="0" AutoGenerateColumns="False" >

   18             <data:DataGrid.Columns>

   19                 <data:DataGridTextColumn Header="Name" FontSize="20" Width="185"

   20                   Binding="{Binding Mode=TwoWay, Path=Name}" />

   21                 <data:DataGridTextColumn Header="Count" FontSize="20" Width="188"

   22                   Binding="{Binding Mode=TwoWay, Path=Count}" />

   23             </data:DataGrid.Columns>

   24         </data:DataGrid>

   38         <charting:Chart x:Name="chart" Grid.Row="2" Grid.Column="0">

   39             <charting:Chart.Series>

   40                 <charting:ColumnSeries

   41                    DependentValueBinding ="{Binding Mode=TwoWay, Path=Count}"

   42                    IndependentValueBinding ="{Binding Mode=TwoWay, Path=Name}" />

   43             </charting:Chart.Series>

   44         </charting:Chart>

   49         <ComboBox x:Name="comboBoxName" Width="150" Height="25" HorizontalAlignment="Left" Margin="10,0,0,0">

   50              <ComboBox.ItemTemplate>

   51                   <DataTemplate>

   52                          <TextBlock Text="{Binding Path=Name}" FontSize="16"/>

   53                   </DataTemplate>

   54              </ComboBox.ItemTemplate>

   55          </ComboBox>

With this binding in place we get out of the box two way data binding, that is, if the underlying collection of person objects is modified it is automatically reflected in the UI elements that are bound to it. Likewise if the UI is changed then the underlying collection is also modified. Now that is pretty cool.

If you are interested in the complete source code, please visit my Silverlight gallery there you can see in action two Silverlight data binding applications. Just select Databinding 1 and Databinding 2 from the menu. The first, demos data binding to simple object and the second, demos data binding to a collection of objects. As always the source code to every demo on the gallery is available for download.

Enjoy!

If you are wondering about the chart and expander controls in Databinding 2 gallery demo then visit this codeplex site.

This post was inspired by Jigar Desai post. Check it out.

Guess the movie

I wish I could do something about this. But I can't. But I can promise you two things. One: I'll always look this good. Two: I'll never give up on you... ever.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Monday, November 03, 2008 4:55 PM
Permalink | Comments (82) | Post RSSRSS comment feed