C# 使用c从windowsphone的列表框中删除所选项目

C# 使用c从windowsphone的列表框中删除所选项目,c#,xaml,windows-phone-8,listbox,C#,Xaml,Windows Phone 8,Listbox,我想使用上下文菜单下拉框从我的列表框中删除所选项目这里是我的xaml <ListBox Margin="3,60,1,10" Grid.Row="1" Name="lstNews" Tap="lstNews_Tap" Width="476" d:LayoutOverrides="VerticalMargin"> <ListBox.ItemTemplate> <DataTemplate>

我想使用上下文菜单下拉框从我的列表框中删除所选项目这里是我的xaml

  <ListBox Margin="3,60,1,10" Grid.Row="1" Name="lstNews" Tap="lstNews_Tap" Width="476" d:LayoutOverrides="VerticalMargin">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Grid.Row="1" Orientation="Horizontal" Height="120" Width="478">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
                    </StackPanel.Background>


                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu>
                            <toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
                            <toolkit:MenuItem Header="Open" Click="MenuItem_Click"/>

                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>


                    <Grid HorizontalAlignment="Left" Width="30" Background="#FF0195D5" Margin="0,0,0,2" Height="118">
                        <TextBlock x:Name="txtDate" TextWrapping="Wrap" Text="{Binding Path=newsDate}" RenderTransformOrigin="0.5,0.5" Margin="-43.169,44.001,-43.831,0" UseLayoutRounding="False" d:LayoutRounding="Auto" TextAlignment="Center" Height="30" VerticalAlignment="Top" Width="117">
                            <TextBlock.RenderTransform>
                                <CompositeTransform Rotation="-90"/>
                            </TextBlock.RenderTransform>
                        </TextBlock>
                    </Grid>
                    <Grid HorizontalAlignment="Left" Width="5" Height="120"/>
                    <StackPanel Orientation="Vertical" VerticalAlignment="Top" Width="432" Height="114">
                        <TextBlock x:Name="txtTitle" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsTitle}" Foreground="Black" FontSize="18.667" HorizontalAlignment="Left" Width="432" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Width="432" Height="27">
                            <TextBlock x:Name="txtBy" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsSource}" Foreground="Black" FontSize="18.667" Width="399"/>
                            <Image x:Name="imgArrow" Width="25" Source="Images/Go-In-Arrow.png" Height="25" Margin="5,0,0,0"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Width="433" Height="60">
                            <TextBlock x:Name="txtDesc" Height="58" TextWrapping="Wrap" Foreground="Black" Text="{Binding Path=newsShortDescription}" FontSize="18.667" Width="371"/>
                            <TextBlock x:Name="txtID" Height="56" Text="{Binding Path=newsID}"  TextWrapping="Wrap" Foreground="Black" FontSize="18.667" Width="8" Visibility="Collapsed"/>
                            <Image x:Name="imgType" Width="35" Source="{Binding Path=newsTypeImage}" Height="40" Margin="27,20,0,0" d:LayoutOverrides="Height"/>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
下面是我试图在delete event MenuItem_Click上执行的操作,但它会引发只读集合上不支持的错误操作。那么,在该Click事件上删除所选项目的代码是什么

 // this is code to delete i am trying-->  lstNews.Items.Remove(lstNews.SelectedItem.ToString());
      //below is code  im trying to set listboxitemsource
          private void FillListBox()
    {
        fulllist = new nList();
        lstNews.ItemsSource = fulllist;

    }

 public class nList : List<NewsData>
{
    StringData sd = new StringData();
    public IList<NewsData> GetLCList()
    {
        IList<NewsData> lcList = null;
        using (NewsDataContext context = new NewsDataContext(sd.news_string))
        {
            IQueryable<NewsData> query = (from app in context.NewsData select app).OrderByDescending(app => app.entryID);
            lcList = query.ToList();
        }
        return lcList;
    }

    public nList()
    {
        IList<NewsData> lcData = this.GetLCList();
        StringBuilder messageBuilder = new StringBuilder();
        foreach (NewsData lc in lcData)
        {

            Add(new NewsData
            {
                newsID = lc.newsID,
                newsTitle = lc.newsTitle,
                newsSource = lc.newsSource,
                newsDate = (new GetDate()).getdate(lc.newsDate),//(new AnnouncementList()).getdate(lc.newsDate),
                newsShortDescription = lc.newsShortDescription,
                newsTypeImage = lc.newsTypeImage,
                newsSharing = lc.newsSharing
            });
        }
    }
}

Items是页面上显示的对象列表。因此,此lstNews.Items是数据模板的集合,这就是为什么尝试lstNews.Items.RemovelstNews.SelectedItem.ToString时失败的原因

您应该使用lstNews.Items.removestnews.SelectedItem删除项目

但为了获得最佳实践,最好是从源中删除项,而不是从列表中删除项。i、 e.您应该从完整列表中删除项目,并将其重新分配为lstNews.ItemsSource=fulllist

代码中所做的更改

fulllist应该是一种类型,以便对数据所做的所有更改都可以反映到UI中。 要将列表转换为ObservableCollection,可以使用以下代码:

fulllist = new ObservableCollection<NewsData>(new nList());

你能把C代码贴出来吗。因为这似乎是lstNews.Items的问题。@PratikGoyal请返回我的c代码。我尝试了你的代码,但它再次抛出错误附加信息:只读集合不支持操作@PratikGoyalcan您可以尝试从完整列表中删除数据,并且您可能需要根据记录的ID搜索所选数据。是的,但是如何删除,您可以检查我正在执行的上述代码,以便将记录添加到完整列表中,那么从完整列表中删除该记录的代码是什么,其中newsID==xx,我是LINQ的新手,请帮助@PratikGoyalPlease检查我的答案是否做了一些修改。
object obj = lstNews.SelectedItem;
if(obj is NewsData){
    fulllist.Remove((NewsData)obj);
    lstNews.ItemsSource = fulllist;
}