0
0

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>以下に<x:Array>定義にてコンボボックスのアイテムを設定する

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

@ WPF 4.5入門 by 大田一希さん
No.3283 / 9985

<DataGridComboBoxColumn>以下に<x:Array>の定義をしてコンボボックスアイテムの定義をしている例が紹介されている。

自分でも実装してみたが、アイテムが表示されなかった。

SelectedValueBindingのところを間違ってSelectedItemBindingとしていたのが失敗の原因だった。
入門者はこういうところがすぐに分からない。

以下は動いたコード。

XAML
<Window x:Class="_170425_t1030_dataGrid.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:_170425_t1030_dataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Height="181" Margin="21,21,0,0" VerticalAlignment="Top" Width="465" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn">
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="シュゾク"
                                        SelectedValueBinding="{Binding Species}"
                                        DisplayMemberPath="Label"
                                        SelectedValuePath="Value">
                        <DataGridComboBoxColumn.ItemsSource>
                        <x:Array Type="{x:Type local:SpeciesComboBoxItem}">
                            <local:SpeciesComboBoxItem Label="不明" Value="Unknown"/>
                            <local:SpeciesComboBoxItem Label="人間" Value="Human"/>
                            <local:SpeciesComboBoxItem Label="アンドロイド" Value="Android"/>
                            <local:SpeciesComboBoxItem Label="ボーグ" Value="Borg"/>
                        </x:Array>
                    </DataGridComboBoxColumn.ItemsSource>                
                </DataGridComboBoxColumn>
            </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;
using System.Collections.ObjectModel;

namespace _170425_t1030_dataGrid
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var data = new ObservableCollection<Member>();
            data.Add(new Member { Name = "7of9", Species = Species.Borg, Age = 20, comment = "1"});
            data.Add(new Member { Name = "Janeway", Species = Species.Human, Age = 20, comment = "2" });
            data.Add(new Member { Name = "Odo", Species = Species.Unknown, Age = 20, comment = "3" });
            dataGrid.ItemsSource = data;
        }

        public class Member
        {
            public string Name { get; set; }
            public Species Species { get; set; }
            public int Age { get; set; }
            public string comment { get; set; }
        }

        private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Name":
                    e.Column.Header = "名前";
                    e.Column.DisplayIndex = 0;
                    break;
                case "Species":
                    e.Column.Header = "種族";
                    e.Column.DisplayIndex = 1;
                    break;
                case "Age":
                    e.Cancel = true;
                    break;
                case "comment":
                    e.Column.Header = "備考";
                    e.Column.DisplayIndex = 2;
                    break;
            }
        }
    }

    public enum Species
    {
        Unknown = 0,
        Human,
        Android,
        Borg,
    }

    public class SpeciesComboBoxItem
    {
        public string Label { get; set; }
        public Species Value { get; set; }
    }
}

work.png

関連

以下では<Window.Resources><x:Array>の定義をしていた。
http://qiita.com/7of9/items/d12402f543400a5de0e1

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?