WPF - ComboBoxの使用方法

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

WPF ComboBoxの使用方法

WPFのComboBoxの使用例。Windows.FormsのComboBoxとは少し違いますね。

Windows1.xaml
<Window x:Class="WpfComboboxBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="130" Width="300">
    <Grid>
        <ComboBox Height="23" Margin="25,32,133,0" Name="comboBox1" VerticalAlignment="Top" />
        <Button Margin="0,33,37,0" Name="button1" Height="22" HorizontalAlignment="Right" VerticalAlignment="Top" Width="76" Click="button1_Click" >Button</Button>
    </Grid>
</Window>
Windows1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
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.ComponentModel;

namespace WpfComboboxBinding
{
    /// 
    /// Window1.xaml の相互作用ロジック
    /// 
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataTable dt = new DataTable("WeekTable");
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("Value", typeof(string));
            dt.Rows.Add("日曜日", "0");
            dt.Rows.Add("月曜日", "1");
            dt.Rows.Add("火曜日", "2");
            dt.Rows.Add("水曜日", "3");
            dt.Rows.Add("木曜日", "4");
            dt.Rows.Add("金曜日", "5");
            dt.Rows.Add("土曜日", "6");

            this.comboBox1.ItemsSource = *1;

        }
    }
}

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

XAMLプログラミング WPFアプリケーションの概要と開発

XAMLプログラミング WPFアプリケーションの概要と開発

*1:IListSource)dt).GetList(); this.comboBox1.DisplayMemberPath = "Text"; this.comboBox1.SelectedValuePath = "Value"; } private void button1_Click(object sender, RoutedEventArgs e) { MessageBox.Show(this.comboBox1.SelectedValue.ToString(