わからんけど
List使ってComboBoxに値を追加したいことありますよね多分。
で、Listにぶち込んでからComboBoxにぶち込み、更にその値をきちんと取得できるかなというテスト。
動くけれども、正しいのかどうかはわからない。
サンプル
XAML
<Window x:Name="window" x:Class="sampleApp.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:sampleApp"
mc:Ignorable="d"
Title="SampleApp" Height="200" Width="300" SizeToContent="WidthAndHeight" ResizeMode="NoResize">
<Grid Width="300" Height="200">
<Grid.RowDefinitions>
<RowDefinition Height="183*"/>
<RowDefinition Height="74*"/>
</Grid.RowDefinitions>
<ComboBox x:Name="comboBox0" Grid.Column="1" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Center" Width="120">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ProductName}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button x:Name="button0" Content="Run" Margin="0,0,0,0" Click="button_Click" Grid.Row="1" Width="100" />
</Grid>
</Window>
C# code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
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 sampleApp
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
private List<GPU> comboList = new List<GPU> { };
public MainWindow()
{
InitializeComponent();
comboList.Add(new GPU
{
Vendor = GPUVendor.NVIDIA,
ProductName = "NVIDIA GTX970"
});
comboList.Add(new GPU
{
Vendor = GPUVendor.NVIDIA,
ProductName = "NVIDIA GTX980"
});
comboList.Add(new GPU
{
Vendor = GPUVendor.AMD,
ProductName = "AMD Radeon HD7770"
});
comboList.Add(new GPU
{
Vendor = GPUVendor.Intel,
ProductName = "Intel HD Graphics 5500"
});
comboBox0.ItemsSource = comboList.ToList();
// LINQを使ってcomboBoxに入れるものをフィルタする場合
/*
comboBox0.ItemsSource = comboList.Where(x => x.Vendor == GPUVendor.NVIDIA).ToList();
*/
}
private void button_Click(object sender, RoutedEventArgs e)
{
GPU temp = (GPU)comboBox0.SelectedItem;
MessageBox.Show("Your select GPU :: " + temp.ProductName);
}
}
public enum GPUVendor { Intel, AMD, NVIDIA };
public class GPU
{
public GPUVendor Vendor { get; set; }
public string ProductName { get; set; }
}
}