Silverlight中为ComboBox设定当前选择项、数据绑定、取值。

public partial class Page : UserControl
 {
  public Page()
  {
   // 需要初始化变量
   InitializeComponent();
            
          Cmbs_Items_Add();
  }

      public class CombItem
        {
            public string Value
            {
                get;
                set;
            }

            public string Text
            {
                get;
                set;
            }


        }

        /// <summary>
        /// 给combox赋值
        /// </summary>
        private void Cmbs_Items_Add()
        {
            //给 房屋标识 赋值
            this.cmbs.UpdateLayout();//设置this.cmbs.UpdateLayout();确保 UIElement 的所有子对象位置都正确地进行了布局更新
            List<CombItem> ctg = new List<CombItem>()
            {
                new CombItem { Value="", Text="" },
                new CombItem { Value="0", Text="正常" },
                new CombItem { Value="1", Text="新增" },
                new CombItem { Value="2", Text="已拆" }
            };


            this.cmbs.ItemsSource = ctg;
            cmbs.DisplayMemberPath = "Text";//绑定Text值
            this.cmbs.SelectedValuePath = "Value";//指定Value值
        }





       #region ========设定当前选择项========
            CombItem emp = new CombItem() { Value = "", Text = "" };                                                                              //this.comboBox1.SelectedItem = emp;  //这样设不起作用.
            List<CombItem> list = this.cmbs.ItemsSource as List<CombItem>;
            int flag = -1;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Value == emp.Value && list[i].Text == emp.Text)
                {
                    flag = i;
                    break;
                }
            }
            this.cmbs.SelectedIndex = flag;
            #endregion

//获取combox选中的值
CombItem emp = this.cmbs.SelectedItem as CombItem;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值