简体   繁体   中英

ObservableCollection didn't Bind the data in listbox

I need to show the WCF service Return Value(LIST) in Silverlight Listbox.

create GetAllAgents Class like,

  public class GetAllAgents
    {
        public List<string> FirstName { get; set; }


        public GetAllAgents(List<string> firstName)
        {
            FirstName = firstName;
        }
    }

The Following Method used for Consume the WCF Service

    public partial class AgentQueue : UserControl
    {
        ChatACDService.ChatACDServiceClient ChatAcdClient = new ChatACDService.ChatACDServiceClient();
        public ObservableCollection<GetAllAgents> _GetAllAgents = new ObservableCollection<GetAllAgents>();

        public AgentQueue()
        {
            InitializeComponent();
            LoadAgentList();
            this.AllList.DataContext = _GetAllAgents;

        }


        private void LoadAgentList()
        {
            ChatAcdClient.GetAllAgentListCompleted += new EventHandler<GetAllAgentListCompletedEventArgs>(ChatAcdClient_GetAllAgentListCompleted);
            ChatAcdClient.GetAllAgentListAsync();


        }
        void ChatAcdClient_GetAllAgentListCompleted(object sender, GetAllAgentListCompletedEventArgs e)
        {
            if (e.Error != null)
            {

            }
            else
            {
                //   AllAgents.ItemsSource = e.Result;
                _GetAllAgents.Add(new GetAllAgents(e.Result.ToList()));
            }
        }

I use the following code For create List Box in XAML page

 <ListBox x:Name="AllList" ItemsSource="{Binding}"
           DisplayMemberPath="FirstName"
        Margin="403,54,0,35" HorizontalAlignment="Left" Width="101" />

But The Output like ,

在此处输入图片说明

I need to show the WCF method's result(return type is list) in Listbox by using ObservableCollection.What are the changes are need to make in above Program?

Actually it works pretty well:

You ask to display the Member Path "FirstName" of your object GetAllAgents. But the Member Path "FirstName" is a list of string.

So your XAML display what you expect from it: the toString() conversion of your memberPath.

And the default toString of your member path which is FirstName which is a list of string is: System.Collection.Generic.List[System.String]

I guess what you expect is that your list of first name should be the item source of your ListBox.

So if your only need is to display their firstName, just replace

public ObservableCollection<GetAllAgents> _GetAllAgents = new ObservableCollection<GetAllAgents>();

By

public ObservableCollection<string> _GetAllAgents = new ObservableCollection<string>();

and

_GetAllAgents.Add(new GetAllAgents(e.Result.ToList()));

By

foreach (var agentName in e.Result.ToList())
{
_GetAllAgents.Add(agentName);
}

And it will display the name of your agent.

If you need mor than that, you will need to create a viewModel per agent object and a dataTemplate to let know Silverlight how you want it to be display.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM