I couldn't find on Google how to solve my problem. I also saw Microsoft Documentation but for some reason it wont work.
I've made a Class for my List with some Propertys
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
public string Count { get; set; }
}
Then I created my List in my Main class.
List<Person> personList = new List<Person>();
Now we come to my problem. I want to check if a item with Name property = "Test" exists. If yes i want to show a MessageBox which returns the result. I tried
if(personList.Find(x => x.Name == "Test"))
Don't work.
if(personList.Find(x => x.Name.Contains("Test"))
Don't work.
Person result = personList.Find(x => x.Name == "Test");
if(result.Name == "Test")
Don't work.
I get messages like i can't convert Person into string/bool. if i try the result one i get the message, that the object was not set to an object instance. I don't understand this Error because i created a instance at the beginning of my Main Class. Also I think that I need a null check. Because I want to check if an Item exists before items are in the list. It's a Event. Full Code of my idea:
TreeViewItem treeViewItem = sender as TreeViewItem;
DockPanel dockpanel = treeViewItem.Header as DockPanel;
List<TextBlock> textblock = dockpanel.Children.OfType<TextBlock>().ToList();
TextBlock name = textblock[0];
TextBlock age = textblock[1];
Person test = personList.Find(x => x.Name == name.Text);
if(test.Name == name.Text)
{
MessageBox.Show(test.Name);
test.Count++;
}
personList.Add(new Person { Name = name.Text, Count = 1, Age = age.Text });
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = personList;