WPF ComboBox DataTemplate Image
<ComboBox ItemsSource="{StaticResource dataList}" SelectedIndex="0" VirtualizingPanel.IsContainerVirtualizable="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"> <ComboBox.ItemTemplate> <DataTemplate> <Image Source="{Binding SelectedItem,RelativeSource= {RelativeSource Mode=FindAncestor,AncestorType={x:Type ComboBox}}}" Stretch="Uniform"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
//xaml <Window x:Class="WpfApp394.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:WpfApp394" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow"> <Window.Resources> <local:DataList x:Key="dataList"/> </Window.Resources> <Grid> <ComboBox ItemsSource="{StaticResource dataList}" SelectedIndex="0" VirtualizingPanel.IsContainerVirtualizable="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling"> <ComboBox.ItemTemplate> <DataTemplate> <Image Source="{Binding SelectedItem,RelativeSource= {RelativeSource Mode=FindAncestor,AncestorType={x:Type ComboBox}}}" Stretch="Uniform"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </Window> //cs 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; namespace WpfApp394 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class DataList:List<string> { public DataList() { var imgsList = Directory.GetFiles(@"../../Images"); if(imgsList!=null && imgsList.Any()) { this.AddRange(imgsList); } } } }