1

我目前有一个按钮的以下代码。消息框显示SilverlightApplication2.ServiceReference2.Employee而不是用户选择的文本字符串。WCF 服务正在填充组合框项。结果,我无法将其传递给 Async 调用。如何获取用户选择的字符串?

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    object selectedItem = comobo1.SelectedItem.ToString();
    MessageBox.Show(selectedItem.ToString());
    var proxy = new Service1Client();
    proxy.GetAllEmployeesCompleted += proxy_GetAllEmployeesCompleted;
    proxy.GetAllEmployeesAsync(selectedItem.ToString());
}

我的服务参考看起来像

公共类下拉{

    [OperationContract]
    public ObservableCollection<Employee> GetAllBrands()
    {   

        var empstwo = new ObservableCollection<Employee>();
        string connect = ConfigurationManager.ConnectionStrings["yoyo"].ToString();
        using (var con = new OdbcConnection(connect))

        {  
            //now you can try
            //wait. To accept a param from main page, u need to create a method to accept that param first.
            //I think you should put this in service1.svc.cs

            string query = "Select distinct(brand) FROM pivottable";
            var cmd = new OdbcCommand(query, con);
            con.Open();
            using (var dr = cmd.ExecuteReader())
            {


                while (dr.Read())
                {
                    var emp = new Employee();

                    emp.ComboData = dr.GetStringOrNull(0);

                    empstwo.Add(emp);

                }


            }



        }

        return empstwo;
    }

}

这是员工类。在此字符串中,ComboData 包含填充我的下拉列表的品牌列表

public class Employee
{
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public Uri ImageURI { get; set; }
    public string ComboData { get; set; }


}
4

3 回答 3

0

你可以使用Text属性。

string selectedText = comobo1.Text;

文档中。

获取或设置当前选定项的文本

于 2014-08-19T03:55:46.000 回答
0

您需要将 selecteditem 转换为您要绑定的对象的类型。像这样的东西,

var selected = (Employee)comobo1.SelectedItem;
MessageBox.Show(selected.ComboData.ToString());
于 2014-08-19T03:55:55.387 回答
0

尝试此代码以编程方式获取 Combobox SelectedItem 的值、文本

ComboboxItem cmb = new ComboboxItem();

cmb = (ComboboxItem)cmb_designation.SelectedItem;

Staffreg.Designation=int.Parse(cmb.Value.ToString());

于 2017-05-30T10:01:26.713 回答