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

时间:2014-09-30 11:11:13

标签: 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>
                <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>

这是我想要删除事件“MenuItem_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
            });
        }
    }
}

1 个答案:

答案 0 :(得分:2)

lstNews.Items是页面上显示的对象列表。所以这个lstNews.Items是你的datatemplate的集合,这就是你尝试lstNews.Items.Remove(lstNews.SelectedItem.ToString())时失败的原因。

您应该使用lstNews.Items.Remove(lstNews.SelectedItem)删除项目。

但是为了最佳实践,首选从源列表中删除项目而不是从列表中删除项目。即,您应该从fulllist删除项目并将其重新指定为lstNews.ItemsSource = fulllist;

代码中的更改

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

    fulllist = new ObservableCollection<NewsData>(new nList());
    
  2. 添加从fulllist删除数据的实现可能的实现可能是:

    object obj = lstNews.SelectedItem;
    if(obj is NewsData){
        fulllist.Remove((NewsData)obj);
        lstNews.ItemsSource = fulllist;
    }