在编辑功能中Combobox和autocompletebox如何设定初始值

Silverlight中有两种下拉框:combobox和autocompletebox,都非常好用,使用ItemsSource绑定下拉框,从selectitem中取得选中的值,但在编辑界面中的下拉框,如何赋值看起来是不方便的?我们知道在winform状态直接设置selectvalue就可以了,下拉中直接显示对应code的name。闲话少说,请看封装的combobox,增加两个属性:

 

public class ComboBoxClassic : ComboBox
    {
        public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ComboBoxClassic), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePathPropertyChanged)));

        public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(ComboBoxClassic), null);

        public ComboBoxClassic(): base()
        {
            base.SelectionChanged += new SelectionChangedEventHandler(ComboBoxClassic_SelectionChanged);
        }


        void ComboBoxClassic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedItem != null && !string.IsNullOrEmpty(SelectedValuePath))
            {
                try
                {
                    SetValue(ComboBoxClassic.SelectedValueProperty, SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem, null));
                }
                catch
                {
                    //Add exception handling
                }
            }
        }

        static void SelectedValuePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }


        public string SelectedValuePath
        {
            get { return (string)GetValue(ComboBoxClassic.SelectedValuePathProperty); }
            set { SetValue(ComboBoxClassic.SelectedValuePathProperty, value); }
        }


        public object SelectedValue
        {
            get
            {
                return GetValue(SelectedValueProperty);
            }
            set
            {
                try
                {
                    var q = (from item in Items
                             where item.GetType().GetProperty(SelectedValuePath).GetValue(item, null).Equals(value)
                             select item).Single();
                    SelectedItem = q;
                }
                catch
                { }
            }
        }

    }

 

 

使用的时候设置如下两个属性即可(以组织下拉框为例):

 

 this.txtOrganization.SelectedValuePath = "OrganizationID";//设定检索的代码

 this.txtOrganization.SelectedValue = user.OrganizationID;//绑定组织的默认值

 

 

以下是autocompletebox的封装:


    public class myAutoCombox : AutoCompleteBox
    {
        public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(myAutoCombox), new PropertyMetadata(new PropertyChangedCallback(SelectedValuePathPropertyChanged)));

        public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(object), typeof(myAutoCombox), null);

        public myAutoCombox()
            : base()
        {
            base.SelectionChanged += new SelectionChangedEventHandler(myAutoCombox_SelectionChanged);
        }


        void myAutoCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedItem != null && !string.IsNullOrEmpty(SelectedValuePath))
            {
                try
                {
                    SetValue(myAutoCombox.SelectedValueProperty, SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem, null));
                }
                catch
                {
                    //Add exception handling
                }
            }
        }

        static void SelectedValuePathPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }


        public string SelectedValuePath
        {
            get { return (string)GetValue(myAutoCombox.SelectedValuePathProperty); }
            set { SetValue(myAutoCombox.SelectedValuePathProperty, value); }
        }


        public object SelectedValue
        {
            get
            {
                return GetValue(SelectedValueProperty);
            }
            set
            {
                try
                {
                    foreach (var item in this.ItemsSource)
                    {
                        if(item.GetType().GetProperty(SelectedValuePath).GetValue(item, null).Equals(value))
                            this.SelectedItem = item;
                    }  
                }
                catch
                { }
            }
        }

    }

 

以上在vs2008, sl3.0下调试通过;当作抛砖引玉,仅作参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值