エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio / WPF > DataGrid > DataGridComboBoxColumn > public enumで定義したリストをXAMLで指定して使う

Last updated at Posted at 2017-04-21
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

Visual Studio / WPF > DataGridView > DataGridComboBoxColumnを使ってみるの続き。

public enumで定義したリストをDataGridComboBoxColumn のItemsSourceに指定してみる。

参考: https://code.msdn.microsoft.com/windowsdesktop/WPF-ComboBoxEnum-ddd26709

  • 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,
    }
}

work.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?