Silverlight中关于ComboBox的绑定使用

在前台页面添加5个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" SelectionChanged="comboBox2_SelectionChanged" VerticalAlignment="Top" Width="120"  />
        <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"  />
    </Grid>

 

后台 代码

 public partial class PathControl : UserControl    

{

        public PathControl()        

   {            

      InitializeComponent();

              InitizePage();

        }

        public void InitizePage()

        {

            #region   comboBox1 最基本的绑定

            ComboBoxItem lbi = new ComboBoxItem();

            lbi.SetValue(ComboBoxItem.ContentProperty, "江苏");

            comboBox1.Items.Add("武汉");

            comboBox1.Items.Add("郑州");

            comboBox1.Items.Add("苏州");

            comboBox1.Items.Add(lbi);

            comboBox1.SelectedItem = lbi;

            #endregion

 

            #region comboBox2 构建数据源 数据源实体必须是 public  

           List<books> _list = new List<books>();

            _list.Add(new books() { Price = 20.0, Name = "C#" });

            _list.Add(new books() { Price = 23.5, Name = "C++" });

            _list.Add(new books() { Price = 10.2, Name = "MFC" });

            _list.Add(new books() { Price = 15.0, Name = "SliverLight" });

            _list.Add(new books() { Price = 15.5, Name = "Asp.Net" });

            _list.Add(new books() { Price = 14.6, Name = "Jquery" });

            _list.Add(new books() { Price = 19.0, Name = "Javascript" });

            this.comboBox2.DisplayMemberPath = "Name";//显示字段名

            this.comboBox2.UpdateLayout();

            this.comboBox2.ItemsSource = _list;//添加数据源

            this.comboBox2.SelectedItem = (from q in this.comboBox2.Items where (q as books).Name == "MFC" select q).FirstOrDefault();//默认选择项

            #endregion

 

            #region comboBox5

            this.comboBox5.DisplayMemberPath = "Name";

            this.comboBox5.SelectedValuePath = "Price";//指定value值

            this.comboBox5.UpdateLayout();

            this.comboBox5.ItemsSource = _list;

            int index = -1;

            for (int i = 0; i < _list.Count; i++)

            {

                if (_list[i].Price == 15.0)

                {

                    index = i; break;

                 }

            }

            this.comboBox5.SelectedIndex = index;//默认选择项

            #endregion

 

 

            #region comboBox3 自定义Item项 及样式

            ComboBoxItem cbi1 = new ComboBoxItem();

            cbi1.Background = new SolidColorBrush(Colors.Yellow);

            cbi1.HorizontalContentAlignment = HorizontalAlignment.Left;

            cbi1.SetValue(ComboBoxItem.ContentProperty,"郑州");

            ComboBoxItem cbi2 = new ComboBoxItem();

            cbi2.Background = new SolidColorBrush(Colors.Green);

            cbi2.HorizontalContentAlignment = HorizontalAlignment.Center;

            cbi2.SetValue(ComboBoxItem.ContentProperty, "上海");

            ComboBoxItem cbi3 = new ComboBoxItem();

            cbi3.Background = new SolidColorBrush(Colors.Blue);

            cbi3.HorizontalContentAlignment = HorizontalAlignment.Right;

            cbi3.SetValue(ComboBoxItem.ContentProperty, "北京");

            this.comboBox3.Items.Add(cbi1);

            this.comboBox3.Items.Add(cbi2);

            this.comboBox3.Items.Add(cbi3);

            this.comboBox3.SelectedItem = cbi1;

            #endregion

            #region  comboBox4  //自定义图片选项

            Image img = new Image();

            img.Source = new BitmapImage(new Uri("1.png",UriKind.Relative));

            Image img1 = new Image();

            img1.Source = new BitmapImage(new Uri("1.png", UriKind.Relative));         

            StackPanel sp = new StackPanel();

            sp.Orientation = Orientation.Horizontal;  

           sp.Children.Add(img);

            sp.Children.Add(img1);

            ComboBoxItem picComboBox = new ComboBoxItem();

            picComboBox.Content = sp;

            this.comboBox4.Items.Add(picComboBox);

            #endregion

        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            books book = (sender as ComboBox).SelectedItem as books;

            MessageBox.Show(book.ToString());

        }

    }    

public class books

    {

        string name;

        public string Name  

       {

            get { return name; }

            set { name = value; }

        }        

   double price;

        public double Price

        {

            get { return price; }

            set { price = value; }

        }

        public override string ToString()//重写tostring()

        {

            return string.Format("书名:{0}  单价:{1}",Name,Price);

        }

    }

 

posted @ 2012-09-12 23:29  贺俊峰  阅读(4890)  评论(0编辑  收藏  举报