In the following code I'm checking if a certain item exists in an ObservableCollection
and if it does, I want to grab its index so I can move that item to the top but I cannot figure it out.
My object:
public class RecentFile
{
public string FileName { get; set; }
public string DateAdded { get; set; }
}
Cod for finding index of item:
if (recentFilesObservableCollection.Any(f => f.FileName == "MyFileName")) {
foreach (RecentFile file in recentFilesObservableCollection) {
if (file.FileName == "MyFileName") {
var x = RecentFilesDataGrid[(recentFilesObservableCollection.IndexOf(file)];
}
}
}
What would be the best way to get the index number if the item exists?
Ultimately what I need to do is...
- Check if item exists
- If it does move it to the top of the list