我正在使用Windows Phone Control工具包中的ContextMenu。想知道如何知道按下列表中的哪个列表项?似乎我可以知道选择了哪个上下文菜单,但我无法知道操作哪个列表项。请帮忙。谢谢!
<DataTemplate x:Key="ListItemTemplate">
<StackPanel Grid.Column="1" VerticalAlignment="Top">
<TextBlock Tag="{Binding Index}" Text="{Binding SName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Add to playlist" Click="Move_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
private void Move_Click(object sender, RoutedEventArgs e)
{
String name = (string)((MenuItem)sender).Header;
// how to know which index of the item is targeted on
}
答案 0 :(得分:11)
我也会推荐MVVM,但它可以简化。无需为每个对象设置ViewModel。关键是将命令绑定到ItemsControl的DataContext(例如ListBox)。
假设您的ItemTemplate用于ListBox,ListBox将其ItemsSource属性绑定到ViewModel。你的xaml看起来像这样:
<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>
您的ViewModel将具有属性Songs
,该属性是模型对象的集合。它还有一个ICommand AddToPlaylistCommand
。正如我said before,我最喜欢的ICommand实现是来自PNP团队的DelegateCommand。
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
}
答案 1 :(得分:7)
答案是:MVVM。不要在代码隐藏中使用事件注册,而是在ViewModel中调用命令。首先,不是绑定到Data对象,而是绑定到ViewModel(或者表示单个列表项的ViewModel,如果您在列表中)。然后,让ViewModel公开一个可以直接在数据绑定VM上调用的Command,而不是使用Click事件。
以下是我的United Nations News OSS WP7应用中的简化示例:(XAML,C#)
<DataTemplate x:Key="ArticleItemDataTemplate">
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Command="{Binding NavigateToArticle}" Header="read article"/>
<toolkit:MenuItem Command="{Binding ShareViaEmail}" Header="share via email"/>
<toolkit:MenuItem Command="{Binding ShareOnFacebook}" Header="share on facebook"/>
<toolkit:MenuItem Command="{Binding ShareOnTwitter}" Header="share on twitter"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<TextBlockText="{Binding Title}">
</StackPanel>
</DataTemplate>
public ICommand ShareOnTwitter
{
get
{
return new RelayCommand(() =>
IoC.Get<ISocialShareService>().ShareOnTwitter(ShareableOnSocialNetwroks));
}
}
public ICommand ShareOnFacebook
{
get
{
return new RelayCommand(() =>
IoC.Get<ISocialShareService>().ShareOnFacebook(ShareableOnSocialNetwroks));
}
}
public ICommand ShareViaEmail
{
get
{
return new RelayCommand(() =>
IoC.Get<ISocialShareService>().ShareViaEmail(ShareableOnSocialNetwroks));
}
}
这是我在Neurons WP7 OSS项目中使用的同一个想法的另一个简化示例:(XAML,C#)
<DataTemplate x:Key="YouTubeVideoItem">
<Grid>
<Button >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu IsZoomEnabled="False">
<toolkit:MenuItem Command="{Binding NavigateToVideo}" Header="play video" />
<toolkit:MenuItem Command="{Binding ViewInBrowser}" Header="open in browser" />
<toolkit:MenuItem Command="{Binding SendInEmail}" Header="share via email" />
<toolkit:MenuItem Command="{Binding FacebookInBrowser}" Header="share on facebook" />
<toolkit:MenuItem Command="{Binding TweetInBrowser}" Header="share on twitter" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding NavigateToVideo}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
<StackPanel Orientation="Horizontal">
<Image Height="90" Source="{Binding ImageUrl}" />
<TextBlock Width="271" Text="{Binding Title}" />
</StackPanel>
</Button>
</Grid>
</DataTemplate>
public ICommand ViewInBrowser
{
get
{
return new RelayCommand(() =>
TaskInvoker.OpenWebBrowser(this.ExternalLink.OriginalString)
);
}
}
public ICommand TweetInBrowser
{
get
{
return new RelayCommand(() =>
IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.WebBrowser, TwitterUri)));
}
}
public ICommand FacebookInBrowser
{
get
{
return new RelayCommand(() =>
IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.WebBrowser, FacebookUri)));
}
}