<Window x:Class="WpfApp100.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:WpfApp100" WindowState="Maximized" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <ListBox x:Name="lbx" ItemsSource="{Binding ImgsCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" BorderBrush="Blue" BorderThickness="3" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Stretch="Uniform"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> using System; using System.Collections.Generic; 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 WpfApp100 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { Color color = (Color)ColorConverter.ConvertFromString("#FFA500"); InitializeComponent(); this.DataContext = this; FillImgList(); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName) { var handler = PropertyChanged; if (handler != null) { handler?.Invoke(this, new PropertyChangedEventArgs(propName)); } } List<string> imgsCollection; public List<string> ImgsCollection { get { return imgsCollection; } set { if (value != imgsCollection) { imgsCollection = value; OnPropertyChanged(nameof(ImgsCollection)); } } } private double zoomLevel = 1; public double ZoomLevel { get { return zoomLevel; } set { if (value != zoomLevel) { zoomLevel = value; OnPropertyChanged(nameof(ZoomLevel)); } } } void FillImgList() { string str = @"..\..\Images"; string fullDir = System.IO.Path.GetFullPath(str); if (Directory.Exists(fullDir)) { var imgs = Directory.GetFiles(fullDir, "*", SearchOption.AllDirectories); ImgsCollection = new List<string>(imgs); } } } }
标签:ItemTemplate,container,Windows,image,System,zoomLevel,imgsCollection,using,publi From: https://www.cnblogs.com/Fred1987/p/18198446