方法一: this.comboBox1.SelectedItem = (from p in this.comboBox1.Items where (p as Person).Age == 20 select p).First(); 方法二: List list = this.comboBoxStatus.ItemsSource as List; int flag = -1; for (int i = 0; i < list.Count; i++) { if (list[i].KindId == PDCConst.ATPStatus.NoConsent) { flag = i; break; } } this.comboBoxStatus.SelectedIndex = flag; ___________________________________________________ 今天用到了ComboBox控件,像往常一样根据以往使用WebForm的DropDownlist或者WinForm中ComboBox控件的经验来使用Silverlight的ComboBox控件, 可是遇到麻烦了。 为ComboBox绑定了某个列表,然后需要根据我当前的值去指定ComboBox的当前选择项。比如说ComboBox绑定了一个List<Employee>. Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->1List<Employee>list =newList<Employee>(){ 2newEmployee(){ EmpID="111", EmpName="1ssssss"}, 3newEmployee(){EmpID="222",EmpName="2dddd"}, 4newEmployee(){EmpID="333",EmpName="3ffff"} 5}; 6this.comboBox1.ItemsSource =list; 7this.comboBox1.DisplayMemberPath ="EmpName"; 现在希望把ID为333的Employee设为当前选择项。不能像以前那样直接Text="3ffff"设定当前值。 在Siverlight中却有些繁琐。具体的代码: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->//this.comboBox1.SelectedItem = emp; //this is not allowed.Employee emp =newEmployee() { EmpID ="333", EmpName ="3ffff"}; List<Employee>list =this.comboBox1.ItemsSource asList<Employee>; intflag =-1; for(inti =0; i |
展示下我页面的那断combobox代码:
<ComboBox x:Name="cbType" SelectedIndex="0" HorizontalAlignment="Left" Margin="66,158,0,0" VerticalAlignment="Top" SelectionChanged="cbType_SelectionChanged" > <ListBoxItem Content="电力工程"/> <ListBoxItem Content="房地产"/> <ListBoxItem Content="水利工程"/> <ListBoxItem Content="矿山"/> <ListBoxItem Content="交通运输"/> <ListBoxItem Content="工业企业"/> </ComboBox> |
然后我就一样样测试的,其中大部分就是调用combobox的SelectionChanged事件进行更改。 无果后 我发现combobox可以添加的items集合有如图六种: 我说死马就当海马医,为何不用其它一种Item类型进行测试 呢,结果把原来的ListItemBox换成了ComboBoxItemk呢。于是乎,之前的这种现象和上面的那个标题的说完全没了。。。