<ComboBox Grid.Row="0" Grid.Column="0" SelectedIndex="0" ItemsSource="{StaticResource booksData}" FontSize="20" Foreground="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Blue" BorderThickness="3" VirtualizingPanel.IsContainerVirtualizable="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.CacheLength="10" VirtualizingPanel.CacheLengthUnit="Item" VirtualizingPanel.VirtualizationMode="Recycling"> <ComboBox.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="10*"/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="{Binding Name}" Grid.Row="0"/> <TextBlock Text="{Binding ImgUrl}" Grid.Row="2"/> <Image Source="{Binding ImgUrl}" Width="200" Height="500" Grid.Row="1"/> </Grid> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
//xaml <Window x:Class="WpfApp391.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:WpfApp391" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:BooksData x:Key="booksData"/> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <ComboBox Grid.Row="0" Grid.Column="0" SelectedIndex="0" ItemsSource="{StaticResource booksData}" FontSize="20" Foreground="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Blue" BorderThickness="3" VirtualizingPanel.IsContainerVirtualizable="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.CacheLength="10" VirtualizingPanel.CacheLengthUnit="Item" VirtualizingPanel.VirtualizationMode="Recycling"> <ComboBox.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="10*"/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="{Binding Name}" Grid.Row="0"/> <TextBlock Text="{Binding ImgUrl}" Grid.Row="2"/> <Image Source="{Binding ImgUrl}" Width="200" Height="500" Grid.Row="1"/> </Grid> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </Window> //cs 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; namespace WpfApp391 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; } } public class BooksData:ObservableCollection<Book> { public BooksData() { var imgsList = Directory.GetFiles(@"../../Images"); if(imgsList==null || !imgsList.Any()) { return; } int imgsCount = imgsList.Count(); for(int i=0;i< imgsCount; i++) { this.Add(new Book() { Id=i+1, Name=$"Name_{i+1}", ImgUrl = $"{imgsList[i]}" }); } } } public class Book { public int Id { get; set; } public string Name { get; set; } public string ImgUrl { get; set; } } }
标签:via,show,Windows,Combobox,imgsList,System,MainWindow,using,public From: https://www.cnblogs.com/Fred1987/p/18424207