如何将列表数据绑定到组合框

时间:2012-04-02 22:01:02

标签: c#-4.0 data-binding silverlight-4.0

我使用包含此方法的WCF服务:

public List<LocationDB> GetLocation()
{
    List<LocationDB> locations = null;
    using (SqlConnection connection = new SqlConnection(conn))
    {
        using (SqlCommand command = new SqlCommand())
        {
            command.Connection = connection;
            command.CommandText = string.Format("Select L_ID ,L_Name from Location");
            connection.Open();
//code to fill the locations list..

我的问题是当我想在我的代码中绑定此方法的结果时,我会执行以下操作。

 void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
            client.GetLocationCompleted += new EventHandler<GetLocationCompletedEventArgs>(client_GetLocationCompleted);
            client.GetLocationAsync();
        }
    }

和:

void client_GetLocationCompleted(object sender, GetLocationCompletedEventArgs e)
    {
        LocationCombo.ItemsSource = e.Result;
        LocationCombo.SelectedValuePath = 
        LocationCombo.DisplayMemberPath =
    }

最后我的LocationDB Class位于asp网站的App_code文件夹中:

     [DataContract]
public class LocationDB
{
    [DataMember]
    public int Lid { get; set; }
[DataMember]
public int SmId { get; set; }

[DataMember]
public string Lname { get; set; }

如何在不在XAML中的代码中绑定SelectedValePath和DisplayMemberPath。 感谢

2 个答案:

答案 0 :(得分:1)

据我所知,你已经拥有了所需的一切,尽管其中一些需要采用不同的顺序。

void client_GetLocationCompleted(object sender, GetLocationCompletedEventArgs e)
{
    LocationCombo.SelectedValuePath = "Lid";
    LocationCombo.DisplayMemberPath = "Lname";
    LocationCombo.ItemsSource = e.Result;
}

答案 1 :(得分:1)

您应该能够将它们中的每一个设置为一个字符串,该字符串代表属性(在您绑定的对象上),分别用作SelectedValuePath和DisplayMemberPath:

LocationCombo.SelectedValuePath = "Lid";
LocationCombo.DisplayMemberPath ="Lname";
LocationCombo.ItemsSource = e.Result.ToList();