■76235 / inTopicNo.2) |
Re[1]: Visual C#でComboBoxのデータの持たせ方 |
□投稿者/ ぶなっぷ (26回)-(2015/06/16(Tue) 18:35:55)
|
ほとんど分かってなさそうな人に答えだけ教えることに意味があるか疑問ですが、
これみて動かして、どうして動くのかちゃんと考えてみてください。
環境の説明もないので、C# + WPF + XAML にて、
[XAML]
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<StackPanel>
<ComboBox x:Name="DataListCombobox"
DisplayMemberPath="Product"
ItemsSource="{Binding DataList}"
SelectedItem="{Binding SelectedData}"
SelectionChanged="DataListCombobox_SelectionChanged" />
<TextBox x:Name="SelectedProduct" />
<TextBox x:Name="SelectedPrice" />
<TextBox x:Name="SelectedArea" />
</StackPanel>
</Window>
[ソースコード]
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 Test
{
public struct Data
{
public string Product { get; set; } // 商品
public int Price { get; set; } // 価格
public string Area { get; set; } // 産地
}
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public List<Data> DataList { get; set; }
public Data SelectedData { get; set; }
public MainWindow()
{
DataList = new List<Data>
{
new Data { Product="果物", Price = 100, Area = "青森"},
new Data { Product="野菜", Price = 80, Area = "千葉"},
new Data { Product="肉", Price = 200, Area = "宮城"},
};
DataContext = this;
InitializeComponent();
DataListCombobox.SelectedIndex = 0;
}
private void DataListCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedProduct.Text = SelectedData.Product;
SelectedPrice.Text = SelectedData.Price.ToString();
SelectedArea.Text = SelectedData.Area;
}
}
}
|
|