SolvedPrivate

Clear ObservableCollection item

Posted on 2014-01-03
3
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

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 63

Expert Comment

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

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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