コンボボックスにListBoxItemを加える方法
コンボボックスの選択を変更するとダイアログ出力
Window1.xaml
<Window x:Class="ProjectName.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid x:Name="grid">
<ComboBox x:Name="cmb" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
Window1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ProjectName
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
protected [[enum]] HotKeys
{
F1 = Key.F1,
F2 = Key.F2,
F3 = Key.F3,
F4 = Key.F4,
F5 = Key.F5,
F6 = Key.F6,
F7 = Key.F7,
F8 = Key.F8,
F9 = Key.F9,
F10 = Key.F10,
F11 = Key.F11,
F12 = Key.F12,
}
public Window1()
{
InitializeComponent();
ListBoxItem item;
foreach (HotKeys key in Enum.GetValues(typeof(HotKeys)))
{
item = new ListBoxItem();
item.Content = key;
this.cmb.Items.Add(item);
}
this.cmb.SelectionChanged += cmb_SelectionChanged;
}
protected void cmb_SelectionChanged(object sender, EventArgs e)
{
Key key = (Key)((ContentControl)this.cmb.SelectedItem).Content;
int keyValue = KeyInterop.VirtualKeyFromKey(key);
MessageBox.Show(String.Format("キー:{0}、キーコード:{1}", key, keyValue));
}
}
}
最終更新:2012年11月07日 00:11