I would also recommend MVVM, but it can be simplified. No need to go to the extreme of having a ViewModel for every object. The key is to bind the command to the DataContext of your ItemsControl (eg ListBox).
Let's assume that your ItemTemplate is for a ListBox, the ListBox has it's ItemsSource property bound to your ViewModel. Your xaml would look like this:
<ListBox x:Name="SongsListBox" ItemsSource="{Binding Songs}">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel >
<TextBlock Text="{Binding SName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Add to playlist" Command="{Binding DataContext.AddToPlaylistCommand, ElementName=SongsListBox}" CommandParameter="{Binding}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Your ViewModel, would then have the property Songs
, that is a collection of your model object. It also has an ICommand AddToPlaylistCommand
. As I've said before, my favorite implementation of ICommand is the DelegateCommand from the PNP team.
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Songs = new ObservableCollection<Songs>();
AddToPlaylistCommand = new DelegateCommand<Song>(AddToPlaylist);
}
public ICollection<Songs> Songs { get; set; }
public ICommand AddToPlaylistCommand { get; private set; }
private void AddToPlaylist(Song song)
{
// I have full access to my model!
// add the item to the playlist
}
// Other stuff for INotifyPropertyChanged
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…