【问题标题】:How to bind List Data to combobox如何将列表数据绑定到组合框
【发布时间】:2012-04-02 22:01:02
【问题描述】:

我是一个包含此方法的 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 =
    }

最后是位于 asp 网站 App_code 文件夹中的 LocationDB 类:

     [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 中。 谢谢

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2010-10-10
        • 1970-01-01
        • 1970-01-01
        • 2021-02-16
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        • 2011-10-14
        相关资源
        最近更新 更多