<Window x:Class="WpfApp99.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:WpfApp99" WindowState="Maximized" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <Style TargetType="{x:Type TextBlock}" x:Key="tbkStyle"> <Setter Property="TextWrapping" Value="Wrap"/> </Style> <Style TargetType="ComboBoxItem" x:Key="cbxItemStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBoxItem}"> <Border Height="100" Width="1300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="100"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <CheckBox Grid.Column="0" IsChecked="{Binding IsSTEM}" IsThreeState="False"/> <TextBlock Grid.Column="1" Text="{Binding Id}" /> <TextBlock Grid.Column="2" Text="{Binding ISBN}" Style="{StaticResource tbkStyle}"/> </Grid> <Border.Background> <ImageBrush ImageSource="{Binding ImgSource}" RenderOptions.BitmapScalingMode="Fant" Stretch="Uniform"/> </Border.Background> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Red"/> <Setter Property="FontSize" Value="30"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <ComboBox Width="1300" Height="100" MaxDropDownHeight="700" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource cbxItemStyle}"/> </Grid> </Window> //cs using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; using System.Text; 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; namespace WpfApp99 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var vm = new BookVM(); this.DataContext = vm; } } public class BookVM : INotifyPropertyChanged { public BookVM() { InitData(); } private void InitData() { var imgs = Directory.GetFiles(@"../../../Images"); if (imgs != null && imgs.Any()) { int imgsCount = imgs.Count(); BooksCollection = new ObservableCollection<Book>(); for (int i = 0; i < imgsCount; i++) { BooksCollection.Add(new Book() { Id = i + 1, IsSelected=false, ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}", ImgSource = GetImageSourceViaUrl(imgs[i%imgsCount]) }); } } } private ImageSource GetImageSourceViaUrl(string imgUrl) { BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource = new Uri(imgUrl, UriKind.RelativeOrAbsolute); bmi.EndInit(); if(bmi.CanFreeze) { bmi.Freeze(); } return bmi; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler != null) { handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } private ObservableCollection<Book> booksCollection; public ObservableCollection<Book> BooksCollection { get { return booksCollection ?? (new ObservableCollection<Book>()); } set { if (value != booksCollection) { booksCollection = value; OnPropertyChanged(); } } } } public class Book { public int Id { get; set; } public bool IsSelected { get; set; } public string ISBN { get; set; } public ImageSource ImgSource { get; set; } } }
标签:via,Windows,ComboBox,bmi,System,ComboxItem,new,using,public From: https://www.cnblogs.com/Fred1987/p/18636889