DataGrid 中Combox的使用
前台代码:
<Window x:Class="WpfApp1.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:core="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" 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 WpfApp1 { public class Customer { public SexEnum sex { get; set; } } public enum SexEnum { 男,女 }; public partial class MainWindow : Window { ObservableCollection<Customer> custdata = new ObservableCollection<Customer>(); public MainWindow() { InitializeComponent(); Customer cm = new Customer(); cm.sex = SexEnum.男; custdata.Add(cm); DG1.DataContext = custdata; } } }