LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio / WPF > ComboBox > Nameの表示にはComboBox.ItemTemplateでなく、DisplayMemberPathを使う

Last updated at Posted at 2017-06-12
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

ComboBoxを再度使ってみる。

Visual Studio / WPF > コントロール > ComboBox > ItemTemplateでの見た目の定義 / IsEditable
http://qiita.com/7of9/items/f63e1150cc88fdf41e12

v0.1

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 _170612_t1340_comboBox
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var data = new List<ComboBoxItem>();
            data.Add(new ComboBoxItem { Name = "7of9" });
            data.Add(new ComboBoxItem { Name = "Janeway" });
            data.Add(new ComboBoxItem { Name = "Tubok" });
            data.Add(new ComboBoxItem { Name = "Chakotay" });
            uxItems.ItemsSource = data;
        }
    }

    public class ComboBoxItem
    {
        public string Name { get; set; }
    }
}

MainWindow.xaml
<Window x:Class="_170612_t1340_comboBox.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:_170612_t1340_comboBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="uxItems" Height="28" Width="100">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Name}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

2017-06-12_13h52_57.png

表示に違和感がある。

v0.2

ComboBoxにEnumを列挙する。
http://qiita.com/maenotti_99/items/4dddbc755efa74086b7c
を見たところ、DisplayMemberPathを使えば良さそう。

<ComboBox Name="cbSample" DisplayMemberPath="Name"/>
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 _170612_t1340_comboBox
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var data = new List<ComboBoxItem>();
            data.Add(new ComboBoxItem { Name = "7of9" });
            data.Add(new ComboBoxItem { Name = "Janeway" });
            data.Add(new ComboBoxItem { Name = "Tubok" });
            data.Add(new ComboBoxItem { Name = "Chakotay" });
            uxItems.ItemsSource = data;
        }
    }

    public class ComboBoxItem
    {
        public string Name { get; set; }
    }
}
MainWindow.xaml
<Window x:Class="_170612_t1340_comboBox.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:_170612_t1340_comboBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="uxItems" Height="28" Width="100"
                  DisplayMemberPath="Name">
        </ComboBox>
    </Grid>
</Window>

2017-06-12_13h55_03.png

違和感がなくなった。

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