<Window x:Class="WpfApp403.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"
xmlns:local="clr-namespace:WpfApp403"
mc:Ignorable="d"
WindowState="Maximized"
AllowsTransparency="True"
WindowStyle="None"
KeyDown="Window_KeyDown"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox x:Name="lbx"
ItemsSource="{Binding Books,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="{Binding SelectedIdx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling"
VirtualizingPanel.CacheLength="1"
VirtualizingPanel.CacheLengthUnit="Item">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding ImgUrl}"
RenderOptions.BitmapScalingMode="Fant"
Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"
Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Width="200"
Height="100"
Foreground="Red"
FontSize="50"
Text="{Binding Id}"
Panel.ZIndex="2"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
//using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;
namespace WpfApp403
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
InitData();
System.Timers.Timer tmr = new System.Timers.Timer();
tmr.Elapsed += Tmr_Elapsed;
tmr.Interval = 1;
tmr.Start();
SizeChanged += MainWindow_SizeChanged;
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
}
private void InitData()
{
Books = new ObservableCollection<Book>();
var imgsList = Directory.GetFiles(@"../../Images");
if (imgsList != null && imgsList.Any())
{
int imgsCount = imgsList.Count();
for (int i = 0; i < 1000000; i++)
{
Books.Add(new Book()
{
Id = i + 1,
ImgUrl = imgsList[i % imgsCount]
});
}
}
}
private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (++SelectedIdx >= books.Count)
{
SelectedIdx = 0;
}
System.Diagnostics.Debug.WriteLine(SelectedIdx);
Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send,new Action(() =>
{
lbx.ScrollIntoView(lbx.SelectedItem);
}));
}
private ObservableCollection<Book> books;
public ObservableCollection<Book> Books
{
get
{
return books;
}
set
{
if (value != books)
{
books = value;
OnPropertyChanged(nameof(Books));
}
}
}
private int selectedIdx;
public int SelectedIdx
{
get
{
return selectedIdx;
}
set
{
if (value != selectedIdx)
{
selectedIdx = value;
OnPropertyChanged(nameof(SelectedIdx));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message);
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
Width = SystemParameters.PrimaryScreenWidth;
Height = SystemParameters.PrimaryScreenHeight;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers == ModifierKeys.Control) && e.Key == Key.C)
{
var msgResult = MessageBox.Show("Are you sure to exit?", "Exit",
MessageBoxButton.YesNo, MessageBoxImage.Question);
if (msgResult == MessageBoxResult.Yes)
{
this.Close();
}
}
}
}
public class BooksData : ObservableCollection<Book>
{
public BooksData()
{
var imgsList = Directory.GetFiles(@"../../Images");
if (imgsList != null && imgsList.Any())
{
int imgsCount = imgsList.Count();
for (int i = 0; i < 10000; i++)
{
this.Add(new Book()
{
Id = i + 1,
ImgUrl = imgsList[i % imgsCount]
});
}
}
}
}
public class Book
{
public int Id { get; set; }
public string ImgUrl { get; set; }
}
}
![]()
![]()
![]()