Visual Studio/WPF > ComboBox > Name을 표시하려면 ComboBox.ItemTemplate 대신 DisplayMemberPath를 사용합니다.

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

ComboBox를 다시 사용해 본다.

Visual Studio/WPF > 컨트롤 > ComboBox > ItemTemplate에서 외형 정의/IsEditable
ぃ tp // m / 7 ~ f9 / ms / f63 1150 ~ c88 fdf 41 12

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>




표시에 위화감이 있다.

v0.2



ComboBox에 Enum을 열거합니다.
ぃ tp // 코 m / 마에노치 _99 / ms / 4 dbc755 에후 74086b7c
를 보면 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>




위화감이 사라졌다.

좋은 웹페이지 즐겨찾기