This is a migrated thread and some comments may be shown as answers.

Binding Combobox from WCF

4 Answers 97 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Vuyiswa
Top achievements
Rank 2
Vuyiswa asked on 11 Feb 2011, 09:43 AM

Good Day Guys , i am using RadSilverlight controls and the implementation should be the same with the normal Combobox. But now the List that i get is not what i want or the data that i need. i have a WCF DataLayer Service that has a function that is defined like this 

public IList<AccountsModel> GetAccounts()
        {
            con = new SqlConnection(strConccounting);
 
            cmdselect = new SqlCommand("usp_Select_Account");
 
            cmdselect.CommandType = CommandType.StoredProcedure;
 
            da = new SqlDataAdapter();
 
            cmdselect.Connection = con;
 
            da.SelectCommand = cmdselect;
 
            DataTable dt = new DataTable();
 
            List<AccountsModel> m_Accounts = new List<AccountsModel>();
            try
            {
                con.Open();
 
                da.Fill(dt);
 
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        AccountsModel m = new AccountsModel();
                        m.iAccountID = Convert.ToInt32(dt.RowsIdea[0]);
                        m.sName = dt.RowsIdea[1].ToString();
                        m_Accounts.Add(m);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return m_Accounts;
 
        }

and now in my Presentation Layer, my Silverlight App , i want to Bind it to the Combobox. Now normally in asp.net . you would have e.g

ID , that will be Primary key for the record and the "Description" and that will be the Text that will be displayed in the the Dropdownlist. So my setup is the same and i have amodel that is sitting on the Data layer that i forgot to share with you

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ServiceModel;
using System.Runtime.Serialization;
 
namespace DAL
{
    public class AccountsModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        private int _iAccountID;
        private string _sName;
 
        /// When the Property is changed from the PL this gets Fired
        /// </summary>
        /// <param name="property"></param>
        private void OnPropertyChanged(String property)
        {
 
            // DAL objConverter = new DAL();
 
            List<AccountsModel> Model = new List<AccountsModel>();
 
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
                //objConverter.Updatedata();               
            }
        }
 
 
        public int iAccountID
        {
 
            get
            {
                return _iAccountID;
            }
            set
            {
                _iAccountID = value;
 
                OnPropertyChanged("iAccountID");
 
            }
        }
 
 
        public string sName
        {
 
            get
            {
                return _sName;
            }
            set
            {
                _sName = value;
 
                OnPropertyChanged("sName");
 
            }
        }
    }
}

ok now lets Continue. Now on the Silverlight side , i had a Telerik Combobox that is defined like this in the xaml

<telerik:RadComboBox HorizontalAlignment="Left"  Text="{Binding sName}" Margin="222,80,0,0" Name="ddlaccount" VerticalAlignment="Top" Width="186" Height="26">
        </telerik:RadComboBox>

and the server side i am creating the BLL Service object and accessing the funtions like this

public Posting()
        {
            InitializeComponent();
 
           Loaded += new RoutedEventHandler(Posting_Loaded);
        }
 
        void Posting_Loaded(object sender, RoutedEventArgs e)
        {
 
            BLL.BLLServiceClient objBLL = new BLL.BLLServiceClient();
 
            
            objBLL.GetAccountsCompleted += new EventHandler<BLL.GetAccountsCompletedEventArgs>(objBLL_GetAccountsCompleted);
            objBLL.GetAccountsAsync();
        }
 
        void objBLL_GetAccountsCompleted(object sender, BLL.GetAccountsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
           
                ddlaccount.ItemsSource = e.Result;
             
            }
        }

and at the same time , i am binding the Combobox. but the Results i get are not the Values but this

Thanks

4 Answers, 1 is accepted

Sort by
0
Dan Andrews
Top achievements
Rank 2
answered on 14 Feb 2011, 05:07 PM
<telerik:RadComboBox Name="ddlaccount"
               t:TextSearch.TextPath="sName"
               SelectedValuePath="sName"
               DisplayMemberPath="sName"
               SelectedValue="{Binding Path=myName, Mode=TwoWay, NotifyOnValidationError=True}"
               IsEditable="True" />

You'll then need to set the datacontext of this to an object that has myName (of type Account).  Generally you don't bind directly to the WCF object, but instead make a Model which exposes and/or extends properties of the WCF object.  Your ViewModel will have a collection of your Model.  It may appear to be duplicate work, but you need the model to do validation which a WCF object does not have.

public class ModelBaseClass : INotifyPropertyChanged, INotifyDataErrorInfo
{
          ....
}
0
Dan Andrews
Top achievements
Rank 2
answered on 14 Feb 2011, 05:13 PM
Your View, Model, ViewModel all belong in the Silverlight application.  What you're calling your Model (on the WCF side) is basically a Data transfer object (DTO), formerly known as value objects or VO.
0
JULIO
Top achievements
Rank 1
answered on 17 Jun 2011, 07:24 PM
If a fill with static info., I can see the items, , but, databinding makes this:
0
JULIO
Top achievements
Rank 1
answered on 23 Jun 2011, 12:02 AM
Im changin the style, but nothing, still can't see anything, but if I click, all this items are overthere.

¿¿¿¿ Is this a BUG ????
Tags
ComboBox
Asked by
Vuyiswa
Top achievements
Rank 2
Answers by
Dan Andrews
Top achievements
Rank 2
JULIO
Top achievements
Rank 1
Share this question
or