今天用到了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>

 

原创贴,自娱自乐的同时,也为大家带来帮助 。