Wpf Combobox display multiple fields columns properties

 <ComboBox Grid.Row="0" x:Name="cbx" VirtualizingPanel.VirtualizationMode="Recycling"
           HorizontalAlignment="Stretch" VerticalContentAlignment="Center" 
           FontSize="30" SelectedIndex="0" >
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <TextBlock Style="{StaticResource tbStyle}">
                 <TextBlock.Text>
                     <MultiBinding StringFormat="{}{0}_____{1}_____{2}">
                         <Binding Path="Id"/>
                         <Binding Path="Name"/>
                         <Binding Path="ReleaseDate"/>
                     </MultiBinding>
                 </TextBlock.Text>
             </TextBlock>
         </DataTemplate>
     </ComboBox.ItemTemplate>
 </ComboBox>

 

 

 

The whole code 

//xaml
<Window x:Class="WpfApp35.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:WpfApp35" WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="tbStyle">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="FontSize" Value="50"/>
                    <Setter Property="FontWeight" Value="ExtraBold"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox Grid.Row="0" x:Name="cbx" VirtualizingPanel.VirtualizationMode="Recycling"
                  HorizontalAlignment="Stretch" VerticalContentAlignment="Center" 
                  FontSize="30" SelectedIndex="0" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Style="{StaticResource tbStyle}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0}_____{1}_____{2}">
                                <Binding Path="Id"/>
                                <Binding Path="Name"/>
                                <Binding Path="ReleaseDate"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </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;

namespace WpfApp35
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            List<Book> booksList = new List<Book>();
            for(int i=0;i<1000;i++)
            {
                booksList.Add(new Book()
                 {
                     Id = i + 1,
                     Name = $"Name_{i + 1}",
                     Author = $"Author_{i + 1}",
                     Concept = $"Concept_{i + 1}",
                     Header = $"Header_{i + 1}",
                     ISBN = $"ISBN_{i + 1}",
                     ReleaseDate = $"ReleaseDate_{DateTime.Now.AddYears(-(i % 1000)).ToString("yyyyMMddHHmmssffff")}",
                     Summary = $"Summary_{i + 1}",
                     Title = $"Title_{i + 1}",
                     Topic = $"Topic_{i + 1}"
                 });
            }
            cbx.ItemsSource = booksList;
        }
    }


    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public int Chapter { get; set; }
        public string Concept { get; set; }
        public string Header { get; set; }
        public string ISBN { get; set; }
        public string ReleaseDate { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
}

 

 

 

posted @ 2024-04-03 14:36  FredGrit  阅读(15)  评论(0编辑  收藏  举报