2

我正在使用 Windows Phone 控制工具包中的 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
    }
4

2 回答 2

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。正如我之前所说,我最喜欢的 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
}
于 2013-01-05T05:24:49.457 回答
7

答案是:MVVM。不要在代码隐藏中使用事件注册,而是在 ViewModel 中调用命令。首先,不是绑定到 Data 对象,而是绑定到 ViewModel(或者如果您在列表中,则绑定到表示单个列表项的 ViewModel)。然后,不要使用 Click 事件,而是让您的 ViewModel 公开一个可以直接在数据绑定 VM 上调用的命令。

这是我的联合国新闻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 项目中使用的相同想法的另一个简化示例:( XAMLC#

    <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)));
        }
    }
于 2013-01-05T00:53:57.807 回答