Silverlight: 为ComboBox实现SelectedValue属性

silverlight中的comboBox并不像一般asp.net的dropdownlist或winform中的comboBox,有selectedValue属性。因为在silverlight中使用comboBox使用selectedItem就基本上满足了需求,如果还不够可以再为相应Tag赋值就OK了。

 

下面主要说一下为comboBox扩展一个selectedValue属性,其实也很简单,只要为ComboBox注册一个就OK。

 

1.  一个简单的效果图:

2010042122442697.png

 

2.  调用方法:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
ComboBoxItemValueExtend cbmValueExtend = new ComboBoxItemValueExtend();
cbmValueExtend.SelectionChanged
+= (sender, arg) =>
{
UI.Alert(cbmValueExtend.SelectedValue);
};
cbmValueExtend.ItemsSource
= DepartmentDataSource();
cbmValueExtend.DisplayMemberPath
= " Name " ;
cbmValueExtend.SelectedValuePath
= " ID " ;

Dialog dlg
= new Dialog()
{
Height
= 300 ,
Width
= 400 ,
Title
= " ComboBox's SelectedValue Sample " ,
Content
= new AutoGrid( 3 , 60 , 180 )
{
Children
=
{
new Label(){Content = " 请选择: " },
cbmValueExtend
}
}
};
dlg.Show();

 

3.  ComboBoxItemValueExtend的实现代码

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace Top.Windows.Settings
{
public class ComboBoxItemValueExtend : ComboBox
{
#region DependencyProperty: SelectedValuePathProperty-----------------------------------------
public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register( " SelectedValuePath " , typeof ( string ), typeof (ComboBoxItemValueExtend), null );

/// <summary>
/// Set the name or path of the property that is value for each date item.
/// </summary>
public string SelectedValuePath
{
get
{
return ( string )GetValue(ComboBoxItemValueExtend.SelectedValuePathProperty);
}
set
{
SetValue(ComboBoxItemValueExtend.SelectedValuePathProperty, value);
}
}
#endregion DependencyProperty: SelectedValuePathProperty-----------------------------------------


#region DependencyProperty: SelectedValueProperty-------------------------------------
public static DependencyProperty SelectedValueProperty = DependencyProperty.Register( " SelectedValue " , typeof ( object ), typeof (ComboBoxItemValueExtend), new PropertyMetadata( new PropertyChangedCallback(SelectedValuePropertyChanged)));

/// <summary>
/// Get Selected Value in the comboBox's SelectedItme
/// </summary>
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
{
throw new Exception();
}
}
}

private static void SelectedValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d
as ComboBoxItemValueExtend).SelectedValue = e.NewValue;
}
#endregion DependencyProperty: SelectedValueProperty-------------------------------------


public ComboBoxItemValueExtend()
:
base ()
{
base .SelectionChanged += new SelectionChangedEventHandler(ComboBoxItemValueExtend_SelectionChanged);
}

void ComboBoxItemValueExtend_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
if (SelectedItem != null && ! string .IsNullOrEmpty(SelectedValuePath))
{
try
{
SetValue(ComboBoxItemValueExtend.SelectedValueProperty, SelectedItem.GetType().GetProperty(SelectedValuePath).GetValue(SelectedItem,
null ));
var value
= SelectedValue;
}
catch
{
throw new Exception();
}
}
}
}
}

 

 

4.  DepartmentDataSource()方法是获取一些测试数据

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
public struct Department
{
public int ID { set ; get ; }
public int ParentId { set ; get ; }
public string Name { set ; get ; }
public string Description { set ; get ; }
}

public List < Department > DepartmentDataSource()
{
List
< Department > list = new List < Department > () {
new Department(){ID = 1 , ParentId = 0 , Name = " Namespace " },
new Department(){ID = 2 , ParentId = 1 , Name = " Class " },
new Department(){ID = 3 , ParentId = 1 , Name = " Enum " },
new Department(){ID = 4 , ParentId = 2 , Name = " Method " },
new Department(){ID = 5 , ParentId = 4 , Name = " Attribute " },
new Department(){ID = 6 , ParentId = 5 , Name = " Private " },
new Department(){ID = 7 , ParentId = 5 , Name = " Publict " },
new Department(){ID = 8 , ParentId = 7 , Name = " DepartmentDataSource() " },
};
return list;
}

 

 

谢谢指导!

 

备注:

关于DependencyProperty的相关知道可以翻阅MSDN学习一下,里面有详细的介绍,这里不再复述!

http://msdn.microsoft.com/zh-cn/library/cc221408.aspx

 

 

转载于:https://www.cnblogs.com/blackcore/archive/2010/04/21/1717655.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值