SolvedPrivate

Clear ObservableCollection item

Posted on 2014-01-03
3
47 Views
Last Modified: 2016-02-10
I have an Observable Collection Called _items. I use it for a chart for when I add series. Each series has an item  in the collection. I need to clear an item in the collection by selecting the series  on the chart which I am able to do and I get the series name. I am using Linq to iterate through each item in the collection and get that Item. With that  in mind how do I clear that entry. Any help would be greatly appreciated
       private void click_me(object sender, ChartItemClickEventArgs e)
        {
            var filteredData = new List<Item>();
//Get the name of the series to delete
            string legendLabel = e.DataSeries.LegendLabel;
            //string strconfirm = "Confirm Deletion of " + legendLabel;
            //MessageBox.Show(strconfirm);
//run though each item in the collection
            foreach (var item in _items)
            {
                if (item != null)
                {
                    if (item.Count > 0)
                    {
//find it
                        var map = viewchart.SeriesMappings.Where(v => legendLabel == item[0].seriesType).FirstOrDefault();
                        if (map == null)
                            return;
//Clear it here
                        
                    }

                }
            }
           

           
        }

Open in new window

0
Comment
Question by:r3nder
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39756165
Hi  r3nder;

This should do what you need.

//run though each item in the collection
// I will assume that _items is a collection of data type Item
Item map = null;
foreach (var item in _items)
{
    if (item != null)
    {
        if (item.Count > 0)
        {
            //find it
            map = viewchart.SeriesMappings
                           .Where(v => legendLabel == item[0].seriesType)
                           .FirstOrDefault();
                           
            if (map == null)
                return;
            //Clear it here
            // Can't remove item while you are iterating through the list              
            // So do it outside the foreach loop
        }
    }
}

_items.Remove(map);

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 39756382
I was trying to  remove it in the find :) thanks man
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39757367
Not a problem, glad to help out.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now