//xaml <Window x:Class="WpfApp90.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:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApp90" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <ObjectDataProvider x:Key="workDayEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type Type="local:WeekDay"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="Id"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock FontSize="30" Foreground="Red" FontWeight="ExtraBold" Text="{Binding Id}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="Name" Binding="{Binding Name}" Foreground="Blue" FontSize="30" FontWeight="ExtraBold"/> <DataGridComboBoxColumn Header="WorkDay" SelectedItemBinding="{Binding WorkDay}" ItemsSource="{Binding Source={StaticResource workDayEnum}}"/> </DataGrid.Columns> </DataGrid> </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 WpfApp90 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); InitCollection(); } private void InitCollection() { var workersList = new List<Worker>(); for(int i=0;i<1000;i++) { Worker wk = new Worker(); wk.Id = i + 1; wk.Name = $"Name_{i + 1}"; wk.WorkDay = GetWeekDayViaValue((i + 1) % 7); workersList.Add(wk); } DataContext = workersList; } public static WeekDay GetWeekDayViaValue(int value) { WeekDay tempDay; switch (value) { case 1: tempDay = WeekDay.Monday; break; case 2: tempDay = WeekDay.Tuesday; break; case 3: tempDay = WeekDay.Wednesday; break; case 4: tempDay = WeekDay.Thursday; break; case 5: tempDay = WeekDay.Friday; break; case 6: tempDay = WeekDay.Saturday; break; default: tempDay = WeekDay.Sunday; break; } return tempDay; } } public class Worker { public int Id { get; set; } public string Name { get; set; } public WeekDay WorkDay { get; set; } } public enum WeekDay { Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7 } }
标签:Windows,Media,System,Datagrid,ObjectDataProvider,using,WPF,MainWindow From: https://www.cnblogs.com/Fred1987/p/18171597