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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 C# 或者 VB.NET 来创建一个窗体应用程序,以下是一个 C# 的示例代码: ```csharp using System; using System.Windows.Forms; namespace ComboBoxDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 初始化 ComboBox 的选项 comboBox1.Items.AddRange(new object[] { "28", "29", "30", "31" }); // 注册事件处理程序 monthComboBox.SelectedIndexChanged += MonthComboBox_SelectedIndexChanged; yearComboBox.SelectedIndexChanged += YearComboBox_SelectedIndexChanged; } private void YearComboBox_SelectedIndexChanged(object sender, EventArgs e) { UpdateDays(); } private void MonthComboBox_SelectedIndexChanged(object sender, EventArgs e) { UpdateDays(); } private void UpdateDays() { int year = int.Parse(yearComboBox.Text); int month = int.Parse(monthComboBox.Text); // 计算天数 int days = DateTime.DaysInMonth(year, month); // 更新 ComboBox 的选项 comboBox1.Items.Clear(); for (int i = 1; i <= days; i++) { comboBox1.Items.Add(i.ToString()); } } } } ``` 在窗体上添加三个控件:ComboBox、Label 和 Button。其中 ComboBox 用于选择天数,Label 用于显示结果,Button 用于触发计算。 在窗体的构造函数中,我们初始化了 ComboBox 的选项,并注册了两个事件处理程序:MonthComboBox_SelectedIndexChanged 和 YearComboBox_SelectedIndexChanged。这两个事件处理程序会在月份或年份发生变化时被触发,然后调用 UpdateDays 方法来更新天数的选项。 UpdateDays 方法会根据当前选择的年份和月份计算天数,并更新 ComboBox 的选项。最后,我们在 Button 的 Click 事件处理程序中获取 ComboBox 的选项,然后显示在 Label 上。 这样,当用户选择不同的年份和月份时,ComboBox 的选项会自动更新,从而实现了天数会随着年份月份的变化而变化的要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值