7/30/2008 9:04:20 PM
Busy, busy with Silverlight lately. I'm starting to really enjoy it and am learning a ton. It's still so new to me that I don't know if I'm doing everything correctly but I am getting stuff to work ;)
The biggest issue I've had with Silverlight lately is databinding and interacting with the bound data. For example, with ASP.NET you can bind a List<Foo> to a Repeater and implement a method for ItemDataBound event that wires up each of the hyperlinks, buttons, etc... to perform certain actions when each Repeater item is databound. In Silverlight you do not have this luxury. The more I work with Silverlight the more I realize that you don't need this.
The way I have been working with Silverlight is the same as working with ASP.NET in that I am a big fan of encapsulation of functionality. This means modularizing a XAML control with a C# code-behind much like an ASCX in ASP.NET. Let's say you have 2 controls: Article, and ArticleList. ArticleList is simply an ItemsControl of Article's. Simple. Here is the XAML:
ArticleListing.xaml
<StackPanel x:Name="stackPanelArticleListing">
<ItemsControl x:Name="itemsControlArticleListing">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Controls:Article x:Name="article" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Simple enough. All the binding syntax is in the Article.xaml. In the Article.xaml I have a TextBox called "textArticle" that displays the entire article.
What if I have a button in my Article control that, when clicked, displays the full article in an ArticleViewer control? It's actually pretty easy if you set the DataContext of the ItemsControl control. I've got another Silverlight Class Library as part of my solution and I've got a class called Article. Article looks like this:
public
class Article
{
public Guid ArticleId { get; set; }
public string Author { get; set; }
public string Body { get; set; }
public string Introduction { get; set; }
public DateTime PublishDate { get; set; }
public string PublishDateShort { get; set; }
public string AuthorUrl { get; set; }
public string AuthorImageUrl { get; set; }
}
So when I go to bind my List<Article> to the ItemsControl I would do this:
itemsControlArticleListing.DataContext = new ArticleLibrary.Article();
var results = ArticleLibrary.Article.RetrieveAllArticles();
itemsControlArticleListing.ItemsSource = results;
So now in my Article control I can get the current DataContext by way of the following:
TextBlock
textBlock = sender as TextBlock;
if (textBlock != null)
{
var article = textBlock.DataContext as ArticleLibrary.Article;
if (article != null)
{
// set another control's text property to article stuff :D
}
}
So I set a breakpoint and you can see that my article object above won't be null:

You can also see that I was able to get the exact object that I clicked on. Well, you really can't see it because of the lack of screen grabs but take my word that this is the one that I clicked on.

Awesome right?
Sorry if this post is all over the place. It's been a work in progress and I've been adding snippets here and there in between work!
C#,
Silverlight
