SL ItemsControl, command on ViewModel not firing from ItemsControl (CheckBox)

Topics: Prism v2 - Silverlight 4, Prism v4 - Silverlight 4
Oct 21, 2010 at 12:22 PM

Hi, 
I'm using PRISM v2, CAL, SL4 and MVVM and have a delegate command on my ViewModel called CheckCommand. The ItemsControl contains a checkbox and I'm trying to get the items in ItemsControl/Checkbox to fire this command when it's checked - but it's not communication back to the viewmodel!

I think it's because each items 'datacontext' is the individual object the item is bound to, rather than the ViewModel?  
- My suspicion is actually correct, cause if I move my DelegateCommand out of the viewmodel and into the class defining the items in itemscontrol I can see the commands/methods beeing fired!

View:

    <ListBox x:Name="BasketListBox" ItemsSource="{Binding BasketCollection}" MinWidth="200">
 <ListBox.ItemTemplate>
  <DataTemplate>
   <CheckBox commands:Checked.Command="{Binding CheckCommand}"  IsChecked="False" </CheckBox>
  </DataTemplate>      
 </ListBox.ItemTemplate>
</ListBox>

Can anyone point me in the right direction please?

Cheers, Mcad.

 

Oct 22, 2010 at 3:54 PM

Hi Mcad,

As you mentioned your suspicion is right. I think that you can find interesting that there is a solution implemented for this scenario in the Stock Trader RI. So, you could take a look at the following files in the StockTraderRI.Modules.Postition.Silverlight:

  • PositionSummaryView\PositionSummaryView.xaml, see the ActionsBuyButton and ActionsSellButton.
  • PositionSummaryView\PositionSummaryView.xaml.cs, see the Model property set.

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero