WPF datagrid combobox 使用枚举

痴心易碎 提交于 2020-07-28 20:38:38
<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 { 男, 女 };
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!