Wpf ComboBox style

FredGrit發表於2024-04-01

<Window x:Class="WpfApp28.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:WpfApp28"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="TextBlock" x:Key="tbkStyle">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="Foreground" Value="Yellow"/>
                    <Setter Property="FontSize" Value="30"/>
                    <Setter Property="FontWeight" Value="ExtraBold"/>
                </Trigger> 
            </Style.Triggers>
        </Style> 
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBlock Text="Select Book" FontSize="20" FontStyle="Italic" FontWeight="UltraBold" />
            <ComboBox x:Name="cbx" ItemsSource="{Binding BooksCollection}"
              DisplayMemberPath="{Binding Name}"   FontSize="20"   IsReadOnly="True" IsEditable="False"
              SelectedValuePath="{Binding Id}" HorizontalAlignment="Left"  
              VerticalAlignment="Center" VerticalContentAlignment="Center" Height="50" 
                      Width="1200" BorderThickness="3" BorderBrush="Red" SnapsToDevicePixels="True"
                      ScrollViewer.HorizontalScrollBarVisibility="Visible" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Style="{StaticResource tbkStyle}">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0}-----{1}-----{2}">
                                    <Binding Path="Id" />
                                    <Binding Path="Name"/>
                                    <Binding Path="Author"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>
    </Grid>
</Window>

  
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
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; 

namespace WpfApp28
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private List<Book> booksCollection;
        public List<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if(value!=booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged("BooksCollection");
                }
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            Init();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propName)
        {
            var handler= PropertyChanged;
            if(handler != null)
            {
                handler.Invoke(this, new PropertyChangedEventArgs(propName));
            } 
        }

        void Init()
        {
            BooksCollection = new List<Book>();
            for(int i=0;i<100;i++)
            {
                BooksCollection.Add(new Book()
                {
                    Id=i+1,
                    Name=$"Name{Guid.NewGuid().ToString()}",
                    Author=$"Author{Guid.NewGuid().ToString()}"
                });
            } 
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public string Description { get; set; }
    }
}

相關文章