Событие из ListBox Scroll C# WPF
Подскажите как создать событие когда вертикальный Scroll в ListBox доходит до конца.
Ответы (1 шт):
Автор решения: RosgardDEM
→ Ссылка
Решение
Допустим, что используется ListView с некоторым списком. Добавляешь ему следующий обработчик:
<ListView ScrollViewer.ScrollChanged="ListView_ScrollChanged">
<!--Something_here-->
</ListView>
В коде обработчика пишешь это:
private void ListView_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (e.ExtentHeight - e.ViewportHeight - e.VerticalOffset == 0)
{
// Enter your code here...
}
}
Пример
XAML:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Title="Scroll to bottom" Height="400" Width="450">
<ListView VirtualizingPanel.ScrollUnit="Pixel"
ScrollViewer.ScrollChanged="ListView_ScrollChanged">
<Rectangle Fill="LightGreen" Height="100" Width="400"/>
<Rectangle Fill="LightCoral" Height="100" Width="400"/>
<Rectangle Fill="LightGreen" Height="100" Width="400"/>
<Rectangle Fill="LightCoral" Height="100" Width="400"/>
<Rectangle Fill="LightGreen" Height="100" Width="400"/>
<Rectangle Fill="LightCoral" Height="100" Width="400"/>
<Rectangle Fill="LightGreen" Height="100" Width="400"/>
<Rectangle Fill="LightCoral" Height="100" Width="400"/>
</ListView>
</Window>
C# Code-behind
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ListView_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (e.ExtentHeight - e.ViewportHeight - e.VerticalOffset == 0)
{
MessageBox.Show("Is bottom!");
}
}
}
}