About Me

indepth experience in developing RIA applications with C# Linq,WCF , Silverlight and JavaScript. Just help in delivering a robust business application in Silverlight for a large financial company.helped with architecture and coding. Participate in Codecamps as a presenter. This blog has no sense of order as it mainly a self help so I can reference things of my interest. my site http://www.sandkeysoftware.com has articles and videos by category

Thursday, July 15, 2010

DataTemplate and Commanding

I have been quiet the last few months as I have been absorbed in a wonderful SilverLight project in the financial sector. However I am near the end and have
some free cycles. I am also working of a Windows 7 phone app and will share a few things that apply to Silverlight in general.

The first is working with SL4 Commanding and MVVM in a DataTemplated UI.Here is my template:


<UserControl.Resources>
local:StockServiceVM x:Key="vm"/>
<DataTemplate x:Key="QuoteTemplate" >
<StackPanel Name="QuoteContainer" Orientation="Vertical" Width="290" Background="Gray">
<StackPanel Orientation="Horizontal">
<TextBlock Foreground="White" Text="{Binding Path= Symbol, Mode=OneWay}" Width="70"/>
<TextBlock Foreground="White" Width="78" Text="{Binding Path=Time}"/>
<TextBlock Foreground="White" Width="80" Text="{Binding Path=Volume}"/>
<Button x:Name="this" Width="20" HorizontalContentAlignment="Right" Content="+"
Command="{Binding Path=ExpandCommand,Source={StaticResource vm}}"
CommandParameter="{Binding ElementName= this}"></Button>
</StackPanel>
</DataTemplate>
</UserControl.Resources>


I am using Commanding to fire the Button click event into my viewmodel and notice the use of the Source property in the Binding definition:
Command="{Binding Path=ExpandCommand,Source={StaticResource vm}}"

If this is omitted my Binding did not work and thus my ViewModel never received the event.

0 comments: