开发者

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

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 view开发者_JAVA百科model!

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


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.


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜