WPF datagrid combobox 使用枚举
<Window x:Class="CY.FQC.Client.demo" 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:CY.FQC.Client" xmlns:core="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="demo" Height="450" Width="800"> <Window.Resources> <ObjectDataProvider x:Key="SexEnumKey" MethodName="GetValues" ObjectType="{x:Type core:Enum}"> <ObjectDataProvider.MethodParameters> <x:Type Type="local:SexEnum"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Grid > <DataGrid Name="DG1" ItemsSource="{Binding}" CanUserAddRows="False" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridComboBoxColumn Header="性别" SelectedItemBinding="{Binding sex}" ItemsSource="{Binding Source={StaticResource SexEnumKey}}" /> </DataGrid.Columns> </DataGrid> </Grid> </Window>
后台:
namespace CY.FQC.Client { /// <summary> /// demo.xaml 的交互逻辑 /// </summary> public partial class demo : Window { public class Customer { public SexEnum sex { get; set; } } public demo() { ObservableCollection<Customer> custdata = new ObservableCollection<Customer>(); InitializeComponent(); Customer cm = new Customer(); cm.sex = SexEnum.男; custdata.Add(cm); DG1.DataContext = custdata; } } public enum SexEnum { 男, 女 }; }