I have a data grid, while scroling when there are less than 50 records left, the event of retrieving subsequent records is triggered. I start the download process in Task.Run.
There is a situation when the user scrolls the scroll bar to the end with the mouse, the program begins to download new records forever. When I run the program through Visual Studio, the interface does not crash and I can interrupt this download by moving the scroll bar up.
But when the program is launched via .exe, the interface is frozen and nothing can be done.
In this case, always e.ExtentHeight - e.VerticalOffset == 27.
I try to set the scroll bar somehow after each call download to get e.ExtentHeight - e.VerticalOffset> 50. But I don't know how to do this for DataGrid I only see ScrollIntoView () but when I used it I not seen any changes.
I made a test program where the same problem occurs when .exe starts:
public partial class MainWindow : Window
{
public List<Event> EventsList { get; set; }
public MainWindow()
{
InitializeComponent();
EventsList = new List<Event>();
}
private void EventDataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
Task.Run(() =>
{
if (e.ExtentHeight - 50 < e.VerticalOffset)
TakeEvents();
});
}
Random r = new Random();
int lastIndex = 0;
private void TakeEvents()
{
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < 900; i++)
{
EventsList.Add(new Event()
{
Device = 1,
Index = lastIndex++
});
}
s.Stop();
RefreshEventsList();
}
private void RefreshEventsList()
{
Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
var filtrList = EventsList.Where(x => x != null && x.Device != 0).ToList();
EventsDataGrid.ItemsSource = filtrList;
});
});
}
}
public class Event
{
public int Index { get; set; }
public int Device { get; set; }
}
XAML:
<Grid>
<DataGrid x:Name="EventsDataGrid" FontSize="15" Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding}" IsReadOnly="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Stretch"
Background="Transparent" Foreground="Black" HeadersVisibility="Column" VerticalScrollBarVisibility="Visible" BorderThickness="0" ScrollViewer.ScrollChanged="EventDataGrid_ScrollChanged" >
<DataGrid.Columns>
<DataGridTextColumn Width="*" Binding="{Binding Index}" Header="Index" Foreground="Black"/>
<DataGridTextColumn Width="*" Binding="{Binding Device}" Header="Device" Foreground="Black"/>
</DataGrid.Columns>
</DataGrid>
</Grid>