Silverlight中关于ComboBox的各种使用

前端放置了几个ComboBox的控件。

1 <Grid x:Name="LayoutRoot" Background="White">
2         <ComboBox Height="23" HorizontalAlignment="Left" Margin="26,49,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
3         <ComboBox Height="23" HorizontalAlignment="Left" Margin="223,49,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox2_SelectionChanged" />
4         <ComboBox Height="23" HorizontalAlignment="Left" Margin="26,140,0,0" Name="comboBox3" VerticalAlignment="Top" Width="120" />
5         <ComboBox Height="23" HorizontalAlignment="Left" Margin="223,140,0,0" Name="comboBox4" VerticalAlignment="Top" Width="120" />
6         <ComboBox Height="23" HorizontalAlignment="Left" Margin="26,199,0,0" Name="comboBox5" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox5_SelectionChanged" />
7     </Grid>

后端的Cs文件如下:

  1 public partial class MainPage : UserControl
  2     {
  3         public MainPage()
  4         {
  5             InitializeComponent();
  6             InitlizePage();
  7         }
  8 
  9         public void InitlizePage()
 10         {
 11             #region comboBox1 最基本的绑定
 12 
 13             ComboBoxItem lbi = new ComboBoxItem();
 14             lbi.SetValue(ComboBoxItem.ContentProperty, "江苏");
 15 
 16             this.comboBox1.Items.Add("武汉");
 17             this.comboBox1.Items.Add("大连");
 18             this.comboBox1.Items.Add("苏州");
 19             this.comboBox1.Items.Add(lbi);
 20 
 21             this.comboBox1.SelectedItem = lbi;
 22             #endregion
 23 
 24             #region 构建了一个数据源
 25             List<Product> _list = new List<Product>();
 26             _list.Add(new Product() { ID = 11, Name = "产品1" });
 27             _list.Add(new Product() { ID = 22, Name = "产品2" });
 28             _list.Add(new Product() { ID = 33, Name = "产品3" });
 29             _list.Add(new Product() { ID = 44, Name = "产品4" });
 30             _list.Add(new Product() { ID = 55, Name = "产品5" });
 31             _list.Add(new Product() { ID = 66, Name = "产品6" });
 32             #endregion
 33 
 34             #region 添加数据源
 35             this.comboBox2.DisplayMemberPath = "Name";
 36             //this.comboBox2.SelectedValuePath = "ID";//没有指定Value值
 37             this.comboBox2.UpdateLayout();
 38             this.comboBox2.ItemsSource = _list;
 39 
 40             //进行初始化赋值,使用SelectedItem  ComboBox的数据初始化跟web中的DropDownList是不一样的
 41             this.comboBox2.SelectedItem = (from p in this.comboBox2.Items
 42                                            where (p as Product).Name == "产品4"
 43                                            select p).First();
 44             #endregion
 45             
 46             #region 
 47             this.comboBox5.DisplayMemberPath = "Name";
 48             this.comboBox5.SelectedValuePath = "ID";//指定Value值
 49             this.comboBox5.UpdateLayout();
 50             this.comboBox5.ItemsSource = _list;
 51 
 52             int SelectedIndex = -1;
 53 
 54             for(int i =0;i<_list.Count;i++ )
 55             {
 56                 if (_list[i].ID == 33)
 57                 {
 58                     SelectedIndex = i;
 59                     break;
 60                 }
 61             }
 62             //通过SelectedIndex来进行初始化绑定
 63             this.comboBox5.SelectedIndex = SelectedIndex;
 64             
 65             #endregion 
 66 
 67 
 68             #region 添加自定义Item
 69             ComboBoxItem cbiRight = new ComboBoxItem();
 70             cbiRight.Background = new SolidColorBrush(Colors.Yellow);
 71             cbiRight.HorizontalContentAlignment = HorizontalAlignment.Right;
 72             cbiRight.SetValue(ComboBoxItem.ContentProperty, "上海");
 73            
 74             ComboBoxItem cbiCenter = new ComboBoxItem(); 
 75             cbiCenter.Background = new SolidColorBrush(Colors.Cyan);
 76             cbiCenter.HorizontalContentAlignment = HorizontalAlignment.Center; 
 77             cbiCenter.SetValue(ComboBoxItem.ContentProperty, "北京");
 78             ComboBoxItem cbiLeft = new ComboBoxItem();
 79             cbiLeft.Background = new SolidColorBrush(Colors.LightGray); 
 80             cbiLeft.HorizontalContentAlignment = HorizontalAlignment.Left;
 81             cbiLeft.SetValue(ComboBoxItem.ContentProperty, "深圳");
 82 
 83             this.comboBox3.Items.Add(cbiRight);
 84             this.comboBox3.Items.Add(cbiCenter);
 85             this.comboBox3.Items.Add(cbiLeft);
 86 
 87             #endregion
 88 
 89             Image img = new Image(); 
 90             img.Source = new BitmapImage(new Uri("img/1.png", UriKind.Relative));
 91 
 92             Label lbl = new Label();
 93             lbl.Content = "带图片的选项";
 94 
 95             StackPanel sp = new StackPanel();
 96             sp.Orientation = Orientation.Horizontal; 
 97             sp.Children.Add(img);
 98             sp.Children.Add(lbl);
 99 
100             ComboBoxItem multipleCmb = new ComboBoxItem(); 
101             multipleCmb.Content = sp;
102             this.comboBox4.Items.Add(multipleCmb);  
103 
104         }
105 
106         private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
107         {
108             Product product = (sender as ComboBox).SelectedItem as Product;
109             int selectID = product.ID;
110             string selectedName = product.Name;
111             //MessageBox.Show(selectID.ToString() + selectedName);
112 
113             Product product2 = comboBox2.SelectedValue as Product;
114             int selectedID2 = product2.ID;
115             string selectedName2 = product2.Name;
116             //MessageBox.Show(selectedID2.ToString() + selectedName2);
117         }
118 
119         //
120         private void comboBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
121         {
122             //MessageBox.Show(((ComboBox)sender).SelectedValue.ToString());//获取value
123             //MessageBox.Show(((Product)((ComboBox)sender).SelectedItem).Name);//获取name呢????
124         }
125     }

当然,其中我们还要构建一个Product的类:

1 public class Product
2     {
3         public int ID { get; set; }
4 
5         public string Name { get; set; }
6     }

获取选择的项的content:

1 string layerName = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content.ToString();
原文地址:https://www.cnblogs.com/qiernonstop/p/3725530.html