0

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:

PRISM-MVVM, ItemsControl problem with View injection

1
  • 1
    This is one of the major limitations of the command pattern. The only way I've been able to get around it is I wrote a custom behavior that similar to the Prism behavior for commanding but it has an option to specify a string name of a command instead of a binding and then it searches up the VisualTree to the ItemsControl and uses its DataContext for binding. Commented Oct 21, 2010 at 16:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
Created seperate question for this problem to make it more clean: stackoverflow.com/questions/4022303/…
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.