前端放置了几个ComboBox的控件。
<GrID x:name="LayoutRoot" Background="White"> <ComboBox Height="23" HorizontalAlignment="left" margin="26,49,0" name="comboBox1" VerticalAlignment="top" WIDth="120" /> <ComboBox Height="23" HorizontalAlignment="left" margin="223,0" name="comboBox2" VerticalAlignment="top" WIDth="120" SelectionChanged="comboBox2_SelectionChanged" /> <ComboBox Height="23" HorizontalAlignment="left" margin="26,140,0" name="comboBox3" VerticalAlignment="top" WIDth="120" /> <ComboBox Height="23" HorizontalAlignment="left" margin="223,0" name="comboBox4" VerticalAlignment="top" WIDth="120" /> <ComboBox Height="23" HorizontalAlignment="left" margin="26,199,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; } }总结
以上是内存溢出为你收集整理的Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)全部内容,希望文章能够帮你解决Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)