2012-04-02 48 views
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類,它是坐落在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; } 

我怎樣才能綁定SelectedValePath和的DisplayMemberPath在代碼中不在XAML中。 謝謝

回答

1

從我可以告訴你已經有你需要的一切,但它需要一些不同的順序。

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

OMG,它的工作原理,我沒有想到會這麼容易..謝謝很多 – AboKevo 2012-04-02 22:16:30

1

你應該能夠給每個人設定爲代表的屬性的字符串(上要綁定到bject)分別作爲SelectedValuePath和的DisplayMemberPath使用方法:

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