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下调试通过;当作抛砖引玉,仅作参考。