0

i want delete selected item from my listbox using context menu drop-down box here is my 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>

here is what i am trying to do on delete event "MenuItem_Click" but than it throws error "Operation not supported on read-only collection.so what is code to delete selected item on that click event

 // 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
            });
        }
    }
}
SD7
  • 514
  • 8
  • 25

1 Answers1

2

lstNews.Items is list of object that are displayed on the page. So this lstNews.Items is collection of your datatemplate so that is why when you tried lstNews.Items.Remove(lstNews.SelectedItem.ToString()) than this fails.

you should use lstNews.Items.Remove(lstNews.SelectedItem) to delete item.

But for best practice it is prefered to delete item from the source not from the list. i.e. You should delete item from fulllist and reassign it as lstNews.ItemsSource = fulllist;

Changes done in your code

  1. fulllist should be a type of ObservableCollection so that all the changes done on data can be reflected to UI. To convert List to ObservableCollection Following code can be used:

    fulllist = new ObservableCollection<NewsData>(new nList());
    
  2. Add the implementation for deleting data from fulllist a possible implementation could be:

    object obj = lstNews.SelectedItem;
    if(obj is NewsData){
        fulllist.Remove((NewsData)obj);
        lstNews.ItemsSource = fulllist;
    }
    
Pratik Goyal
  • 294
  • 1
  • 9
  • hi tried your code but it again throws error Additional information: Operation not supported on read-only collection. @PratikGoyal – SD7 Sep 30 '14 at 12:16
  • can you try removing data from your Fulllist and for that you might need to search selected data may be by the ID of the record. – Pratik Goyal Sep 30 '14 at 12:30
  • yes but how to delete, you can check my above code i am doing for adding record to fulllist, so what will be code for removing that from fulllist where newsID==xx, i am new to LINQ please help @PratikGoyal – SD7 Oct 01 '14 at 05:14
  • Please check I have done some modification in my answer. – Pratik Goyal Oct 01 '14 at 11:30