My blog has moved!

Visit http://blog.adamroderick.com/ and update your bookmarks.

Thursday, July 15, 2010

Download to Excel Behavior for Silverlight DataGrid

I wrote a behavior to export a DataGrid to Excel.  Silverlight allows saving files, and this behavior transforms a DataGrid into a .csv file.

To use, add a reference to your project to the assembly, and you will see the DownloadToExcel behavior under the Assets tab:

assets

Add the behavior to any UIElement.  In this case, I added it to a button:

addedBehavior

The default event selected on the button is the Click event, which is what we want.  Set the Target Object by clicking on the little target icon and then choosing the data grid you want to export to excel:

chooseTarget

Below is a screenshot of the test app.  When we run it and click the button, we are prompted to save the file.  This is the DataGrid in the sample code:

datagrid

The resulting .CSV file--which opens in Excel by default--it looks like this:

excel

 

Download just the .DLL or the source code with sample app.

Thanks to stackoverflow and those who responded to this question for giving me insight.

Labels: ,

Tuesday, July 13, 2010

Viewing WCF Binary-encoded messages in Fiddler

RIA Services (because it is based on WCF) binary encodes the messages it sends across.  These are a pain to try and read when using Fiddler. 

MSDN has a plugin for Fiddler that decodes the message and displays the decoded XML:

FileDownload[1]

Super simple to use, just download and drop the DLL in the Inspectors folder of the Fiddler installation folder.  Details here, and download directly here.

Labels: ,

Friday, July 9, 2010

Extension Method to Populate an ObservableCollection

I have been building a business application using the model-view-viewmodel pattern and RIA Services in Silverlight 4.  Typically, a viewmodel class will make a call to one of the services to get some information.  If this information is a collection to be displayed, I will populate an ObservableCollection property with the collection of items. 

When Silverlight first loads the control, the UI bindings subscribe to the property changed events that the ObservableCollection property fires anytime items are added or removed from the collection.  If I populate my ObservableCollection property like the following, it is a new object on the heap, and the UI binding is not subscribed to anything on the new object:

this.ObservableCollectionProperty = new ObservableCollection(collectionReturnedFromService);

To make things work the way I wanted, I had to call Clear on the existing ObservableCollection and then add each item.  This was common enough that it merited an extension method:

Labels: