Visual Studio/WPF > DataGrid > DataGridComboBoxColumn > public enum에서 정의한 목록을 XAML로 지정하여 사용

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

Visual Studio / WPF > DataGridView > DataGridComboBoxColumn을 사용해보기 계속.

public enum으로 정의한 리스트를 DataGridComboBoxColumn의 ItemsSource로 지정해 본다.

참고 : htps : // 여기. msd 응. mic로소 ft. 코 m / 우동 ws에서 sk와 p / WPF - 코 m 어리석은 m-d d26709
  • CS 파일에서 GenderEnum을 public enum으로 정의합니다.
  • Window.Resources에서 ObjectDataProvider에서 GenderEnum을 정의합니다.
  • GenderEnum을 DataGridComboBoxColumn의 ItemsSource로 지정

  • XAML
    <Window x:Class="_170421_t1510_WpfDataGridComboBox.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:_170421_t1510_WpfDataGridComboBox"
            xmlns:sys="clr-namespace:System;assembly=mscorlib" 
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ObjectDataProvider x:Key="GenderEnum" MethodName="GetValues" 
                            ObjectType="{x:Type sys:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="local:GenderEnum"/>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </Window.Resources>
    
        <Grid>
            <DataGrid x:Name="dataGridView1" HorizontalAlignment="Left" Height="211" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                    <DataGridComboBoxColumn Width="100" x:Name="Gender"
                                            Header="Gender"
                                            ItemsSource="{Binding Source={StaticResource GenderEnum}}"
                                            SelectedValueBinding="{Binding Gender, Mode=TwoWay}"
                                            DisplayMemberPath="{Binding Gender}"/>
                </DataGrid.Columns>
            </DataGrid>
    
        </Grid>
    </Window>
    

    MainWindow.xaml.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 _170421_t1510_WpfDataGridComboBox
    {
        /// <summary>
        /// MainWindow.xaml の相互作用ロジック
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                dataGridView1.Items.Add(new Employee { Name = "7of9", Gender = GenderEnum.Female });
                dataGridView1.Items.Add(new Employee { Name = "Chakotay", Gender = GenderEnum.Male });
                //dataGridView1.Items.Add(new Employee { Name = "Odo", Gender = "Unknown" });
            }
        }
        public class Employee
        {
            public string Name { get; set; }
            public GenderEnum Gender { get; set; }
        }
        public enum GenderEnum
        {
            Male = 0,
            Female,
        }
    }
    

    좋은 웹페이지 즐겨찾기