views:

21

answers:

2

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>

Can anyone point me in the right direction please?

Cheers, Mcad.

EDIT 1:

The commanding now works, see solution below. BUT, I now run into another problem:
"An exception occurred while creating a region with name 'basketRegion'. The exception was: System.InvalidOperationException: ItemsControl's ItemsSource property is not empty. This control is being associated with a region, but the control is already bound to something else. If you did not explicitly set the control's ItemSource property, this exception may be caused by a change in the value of the inherited RegionManager attached property"

Created seperate question for this problem to make it more clean:

http://stackoverflow.com/questions/4022303/prism-mvvm-itemscontrol-problem-with-view-injection

+1  A: 

You want every CheckBox to fire the same command? You could:

<CheckBox commands:Checked.Command="{Binding DataContext.CheckCommand, ElementName=BasketListBox}"

Or you could have every child view model expose the command via their own property.

HTH,
Kent

Kent Boogaart
My post below every now and then gives me the following error while running my code: An exception occurred while creating a region with name 'basketRegion'. The exception was: System.InvalidOperationException: ItemsControl's ItemsSource property is not empty. This control is being associated with a region, but the control is already bound to something else. If you did not explicitly set the control's ItemSource property, this exception may be caused by a change in the value of the inherited RegionManager attached property.
Mcad001
I guess the problem is that I both bind the ItemsSource of the listbox to basketcollection and then I bind "{Binding DataContext.CheckCommand, ElementName=basketListBox}"... But If I remove the ItemsSource I cannot populate my Listbox, any idea how to solve this?
Mcad001
Created seperate question for this problem to make it more clean: http://stackoverflow.com/questions/4022303/prism-mvvm-itemscontrol-problem-with-view-injection
Mcad001
A: 

Thanx Kent. You put me on the right path to solve this, ended up doing this:

<ListBox x:Name="basketListBox" ItemsSource="{Binding basketcollection}" MinWidth="200">
<ListBox.ItemTemplate>
<DataTemplate>
        <CheckBox commands:Checked1.Command="{Binding DataContext.CheckCommand, ElementName=basketListBox}" Content="{Binding basketName}">             </CheckBox>
    </DataTemplate>       
</ListBox.ItemTemplate>

Mcad001