
今天用到了ComboBox控件,像往常一样根据以往使用WebForm的DropDownList或者WinForm中ComboBox控件的经验来使用Silverlight的ComboBox控件,
可是遇到麻烦了。
为ComboBox绑定了某个列表,然后需要根据我当前的值去指定ComboBox的当前选择项。比如说ComboBox绑定了一个List<Employee>.
1 List < Employee > List = new List < Employee > (){2 new Employee(){ EmpID = " 111 " ,Empname = " 1ssssss " },
3 new Employee(){EmpID = " 222 " ,Empname = " 2dddd " },
4 new Employee(){EmpID = " 333 " ,Empname = " 3ffff " }
5 };
6 this .comboBox1.ItemsSource = List;
7 this .comboBox1.displayMemberPath = " Empname " ; 复制代码
现在希望把ID为333的Employee设为当前选择项。不能像以前那样直接Text="3ffff"设定当前值。
在Siverlight中却有些繁琐。具体的代码:
Employee emp = new Employee() { EmpID = " 333 " ,Empname = " 3ffff " }; //this.comboBox1.SelectedItem = emp; //这样设不起作用.List < Employee > List = this .comboBox1.ItemsSource as List < Employee > ;
int flag = - 1 ;
for ( int i = 0 ; i < List.Count; i ++ )
{
if (List[i].EmpID == emp.EmpID && List[i].Empname == emp.Empname)
{
flag = i;
break ;
}
}
this .comboBox1.Selectedindex = flag; 复制代码
到此为止,可以设置ComboBox的当前选择项了。是不是有些绕,为什么不能直接公开一个属性让开发者去设呢?
完整代码:
C#using System;
using System.Collections.Generic;
using System.linq;
using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
using System.Collections;
namespace SilverlightApplication2
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
List<Employee> List = new List<Employee>(){
new Employee(){ EmpID="111",Empname="1ssssss"},
new Employee(){EmpID="222",Empname="2dddd"},
new Employee(){EmpID="333",Empname="3ffff"}
};
this.comboBox1.ItemsSource = List;
this.comboBox1.displayMemberPath = "Empname";
}
private voID btnShow_Click(object sender,RoutedEventArgs e)
{
Employee emp = this.comboBox1.SelectedItem as Employee;
MessageBox.Show(string.Format("EmpID={0},Empname={1}",emp.EmpID,emp.Empname));
}
private voID btnSet_Click(object sender,RoutedEventArgs e)
{
//this.comboBox1.SelectedItem = emp; //this is not allowed.
Employee emp = new Employee() { EmpID = "333",Empname = "3ffff" };
List<Employee> List = this.comboBox1.ItemsSource as List<Employee>;
int flag = -1;
for (int i = 0; i < List.Count; i++)
{
if (List[i].EmpID == emp.EmpID && List[i].Empname == emp.Empname)
{
flag = i;
break;
}
}
this.comboBox1.Selectedindex = flag;
}
}
public class Employee
{
public string Empname { get; set; }
public string EmpID { get; set; }
}
}
XAML
<UserControl x:Class="SilverlightApplication2.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WIDth="400" Height="500">
<StackPanel x:name="LayoutRoot" Background="White">
<ComboBox x:name="comboBox1" WIDth="200" Height="60" margin="20"></ComboBox>
<button x:name="btnShow" Click="btnShow_Click" Content="Get ComboBox SelectItem" Height="60" WIDth="200" margin="20"></button>
<button x:name="btnSet" Click="btnSet_Click" Content="Set ComboBox Selectedindex" Height="60" WIDth="200" margin="20"></button>
</StackPanel>
</UserControl>
原创贴,自娱自乐的同时,也为大家带来帮助 。
http://www.cnblogs.com/charles-chenwei/archive/2009/11/04/1595650.html
总结以上是内存溢出为你收集整理的Silverlight中为ComboBox设定当前选择项,真麻烦全部内容,希望文章能够帮你解决Silverlight中为ComboBox设定当前选择项,真麻烦所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)