0
1

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 > コントロール > CombBox > Eratta: SelectedIndex, SelectedItem, Textの表示コード

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.4344 / 9985

以下のように設定した、ComboBoxコントロールのSelectedIndexプロパティ、SelectedItemプロパティ、Textプロパティを表示するプログラムの実行結果を示します。

上記の文章の下にあるコードは実行結果を再現するようなコードになっておらず、前の方に出てきた別のコードの例が紹介されている。
掲載ミスだろうか。

実行結果の画像からコードを考えてみた。

XAML
<Window x:Class="_170425_t2100_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:_170425_t2100_combobox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox x:Name="comboBox1"
                  Grid.Row="0" Height="40" SelectionChanged="comboBox1_SelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBox Text="{Binding Name}"/>
                        <TextBox Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <StackPanel Grid.Row="1">
            <TextBox Name="T_selectedIndex"/>
            <TextBox Name="T_selectedItem"/>
            <TextBox Name="T_text"/>
        </StackPanel>
    </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 _170425_t2100_combobox
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var data = new List<Person>();
            data.Add(new Person { Name = "item1", Age = 10 });
            data.Add(new Person { Name = "item2", Age = 20 });
            data.Add(new Person { Name = "item3", Age = 30 });

            comboBox1.ItemsSource = data;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            T_selectedIndex.Text = "SelectedIndex:" + comboBox1.SelectedIndex.ToString();
            T_selectedItem.Text = "SelectedItem:" + comboBox1.SelectedItem.ToString();
            T_text.Text = "Text:" + comboBox1.Text;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

work.png

Textの表示結果が違うかもしれない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?