SilverlightでのComboBoxの様々な使用について(基本的には上の方法をまとめています)
11385 ワード
フロントエンドにはいくつかのComboBoxのコントロールが配置されています.
バックエンドのCsファイルは次のとおりです.
もちろん、Productのクラスも構築します.
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="26,49,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="223,49,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox2_SelectionChanged" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="26,140,0,0" Name="comboBox3" VerticalAlignment="Top" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="223,140,0,0" Name="comboBox4" VerticalAlignment="Top" Width="120" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="26,199,0,0" Name="comboBox5" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox5_SelectionChanged" />
</Grid>
バックエンドのCsファイルは次のとおりです.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
InitlizePage();
}
public void InitlizePage()
{
#region comboBox1
ComboBoxItem lbi = new ComboBoxItem();
lbi.SetValue(ComboBoxItem.ContentProperty, " ");
this.comboBox1.Items.Add(" ");
this.comboBox1.Items.Add(" ");
this.comboBox1.Items.Add(" ");
this.comboBox1.Items.Add(lbi);
this.comboBox1.SelectedItem = lbi;
#endregion
#region
List<Product> _list = new List<Product>();
_list.Add(new Product() { ID = 11, Name = " 1" });
_list.Add(new Product() { ID = 22, Name = " 2" });
_list.Add(new Product() { ID = 33, Name = " 3" });
_list.Add(new Product() { ID = 44, Name = " 4" });
_list.Add(new Product() { ID = 55, Name = " 5" });
_list.Add(new Product() { ID = 66, Name = " 6" });
#endregion
#region
this.comboBox2.DisplayMemberPath = "Name";
//this.comboBox2.SelectedValuePath = "ID";// Value
this.comboBox2.UpdateLayout();
this.comboBox2.ItemsSource = _list;
// , SelectedItem ComboBox web DropDownList
this.comboBox2.SelectedItem = (from p in this.comboBox2.Items
where (p as Product).Name == " 4"
select p).First();
#endregion
#region
this.comboBox5.DisplayMemberPath = "Name";
this.comboBox5.SelectedValuePath = "ID";// Value
this.comboBox5.UpdateLayout();
this.comboBox5.ItemsSource = _list;
int SelectedIndex = -1;
for(int i =0;i<_list.Count;i++ )
{
if (_list[i].ID == 33)
{
SelectedIndex = i;
break;
}
}
// SelectedIndex
this.comboBox5.SelectedIndex = SelectedIndex;
#endregion
#region Item
ComboBoxItem cbiRight = new ComboBoxItem();
cbiRight.Background = new SolidColorBrush(Colors.Yellow);
cbiRight.HorizontalContentAlignment = HorizontalAlignment.Right;
cbiRight.SetValue(ComboBoxItem.ContentProperty, " ");
ComboBoxItem cbiCenter = new ComboBoxItem();
cbiCenter.Background = new SolidColorBrush(Colors.Cyan);
cbiCenter.HorizontalContentAlignment = HorizontalAlignment.Center;
cbiCenter.SetValue(ComboBoxItem.ContentProperty, " ");
ComboBoxItem cbiLeft = new ComboBoxItem();
cbiLeft.Background = new SolidColorBrush(Colors.LightGray);
cbiLeft.HorizontalContentAlignment = HorizontalAlignment.Left;
cbiLeft.SetValue(ComboBoxItem.ContentProperty, " ");
this.comboBox3.Items.Add(cbiRight);
this.comboBox3.Items.Add(cbiCenter);
this.comboBox3.Items.Add(cbiLeft);
#endregion
Image img = new Image();
img.Source = new BitmapImage(new Uri("img/1.png", UriKind.Relative));
Label lbl = new Label();
lbl.Content = " ";
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;
sp.Children.Add(img);
sp.Children.Add(lbl);
ComboBoxItem multipleCmb = new ComboBoxItem();
multipleCmb.Content = sp;
this.comboBox4.Items.Add(multipleCmb);
}
private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Product product = (sender as ComboBox).SelectedItem as Product;
int selectID = product.ID;
string selectedName = product.Name;
//MessageBox.Show(selectID.ToString() + selectedName);
Product product2 = comboBox2.SelectedValue as Product;
int selectedID2 = product2.ID;
string selectedName2 = product2.Name;
//MessageBox.Show(selectedID2.ToString() + selectedName2);
}
//
private void comboBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//MessageBox.Show(((ComboBox)sender).SelectedValue.ToString());// value
//MessageBox.Show(((Product)((ComboBox)sender).SelectedItem).Name);// name ????
}
}
もちろん、Productのクラスも構築します.
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
}