如何获取Combobox.SelectedItem返回所选内容的字符串值而不是服务引用类?

时间:2014-08-19 03:39:27

标签: c# wcf combobox pivotviewer

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

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; }


}

3 个答案:

答案 0 :(得分:0)

您可以使用Text属性。

string selectedText = comobo1.Text;

来自documentation

  

获取或设置当前所选项目的文字

答案 1 :(得分:0)

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

var selected = (Employee)comobo1.SelectedItem;
MessageBox.Show(selected.ComboData.ToString());

答案 2 :(得分:0)

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

ComboboxItem cmb = new ComboboxItem();

cmb =(ComboboxItem)cmb_designation.SelectedItem;

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