Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)
来源:互联网 发布:python 3.5 idle 编辑:程序博客网 时间:2024/04/30 00:32
前端放置了几个ComboBox的控件。
<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; } }
- Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)
- ORCL数据库中进行一些相应的操作 基本上就满足的了一般的需求 ,以下是详细步骤>>
- Silverlight中使用MVVM方式进行datagrid的内嵌combobox方法
- Silverlight的空间Combobox的初步使用)
- Silverlight中ComboBox的数据项刷新
- 关于使用BoundsCheck的一些问题汇总
- C# 关于 ComboBox的使用
- C#关于ComboBox的使用
- C#关于ComboBox的使用
- C#关于ComboBox的使用
- Silverlight中使用配置文件的方法
- Silverlight中使用Timer的方法
- 关于FLEX3中的ComboBox的一些问题
- 解决关于iis 中使用 word COM+ 组件的问题 ,应该基本上 都是 权限授权的问题
- 基本上包含了Foudation框架内的所有函数和方法。 使用方法:全选,自己建一个文本类的文件,如果要使用某个方法却想不起来方法名,直接在文本中搜索关键字即可。
- SQL CLR的一些补充(本文基本上是书中的要点摘录)
- Springmvc(四)各种方法的汇总
- 一篇关于在COMBOBOX中使用SENDMESSAGE的实例
- Linux Xclock时针校准错误解决
- VS2008提示无法打开包括文件:“afxcontrolbars.h”解决办法
- 基于Linux开源VOIP系统LinPhone[三]
- pageEncoding与contentType的区别
- RHEL6系统安装教程
- Silverlight中关于ComboBox的各种使用(基本上是汇总了往上的一些方法)
- TCP协议传输的三次握手原理
- asp.net上传文件功能
- Java 相关整理
- Eclipse+CDT+gdb调试android ndk程序
- Cisco子网划分原理基础渗透
- 完全卸载oracle
- 优化pxe网启动时tftp的传输速度 --- 针对pxelinux和bootmgr
- Linux下网络IP地址的转换函数(点分式--长整型转换)