Reputation: 3262
I'm using M-V-VM and have a command on my ViewModel called 'EntitySelectedCommand'.
I've trying to get all the Items in an ItemsControl to fire this command, however it's not working.
I think it's because each items 'datacontext' is the individual object the item is bound to, rather than the ViewModel?
Can anyone point me in the right direction please?
Cheers,
Andy
<ItemsControl ItemsSource="{Binding Path=LinkedSuppliers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Controls:EntityLabel Grid.Column="0" Grid.Row="0" Content="{Binding Name}" CurrentEntity="{Binding }" EntitySelected="{Binding EntitySelectedCommand}" ></Controls:EntityLabel>
<StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Upvotes: 6
Views: 1944
Reputation: 178790
Your suspicion is correct. You have a couple of options:
EntitySelectedCommand
from your child view model as well (ie. each Supplier
would have this property, too).RelativeSource
to reach out and use the DataContext
of the parent ItemsControl
.Upvotes: 13
Reputation: 17143
Have a look at the MVVM Toolkit... It has this idea of a command refrence which you can use!
Create a CommandRefrece as a resource and then just use the StaticResource markup extension...
<c:CommandRefrence x:Key="EntitySelectedCommandRef" Command="{Binding EntitySelectedCommand}" />
and then later you can use
...Command="{StaticResource EntitySelectedCommandRef}" ...
Upvotes: 2