Why does a DataGrid call a Linq query when scrolling?

I am just starting out with Linq, WPF and Silverlight. I am trying to display data that comes from an XML document in a DataGrid. I use the Linq query to select the objects I want and associate the result with a DataGrid.

XDocument doc = GedView.GedcomConverter.ConvertToXml(new StreamReader(e.Result));
var query = from person in doc.Descendants("INDI")
            select new PersonInfo()
            {
              Id = (string)person.Attribute("Value"),
              GedcomName = (string)person.Descendants("NAME").SingleOrDefault().Attribute("Value"),
              Sex = (string)person.Descendants("SEX").SingleOrDefault().Attribute("Value"),
              BirthDate = GedcomConverter.ConvertDate(person.Descendants("BIRT").SingleOrDefault()),
              DeathDate = GedcomConverter.ConvertDate(person.Descendants("DEAT").SingleOrDefault()),
              BurialDate = GedcomConverter.ConvertDate(person.Descendants("BURI").SingleOrDefault()),
            };
DataGrid.ItemsSource = query;
DataGrid.SelectedIndex = -1;

However, when the grid scrolls, performance is poor. I notice that the ConvertDate method is called many times. (The ConvertDate method converts a human-readable date string to a DateTime?).

Why is this? I suggested that the "request" will be executed once, and not continuously.

What will be the correct way? I use the query because I want to add some kind of filter to restrict the items in the list later.

thanks

+3
1

Try: -

DataGrid.ItemsSource = query.ToList();

DataGrid , IEnumerable, , - , . , DataGrid, , GetEnumerator.

, ItemsSource .

+4

Source: https://habr.com/ru/post/1709847/


All Articles