Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Databinding driving me crazy!

Databinding driving me crazy!

Scheduled Pinned Locked Moved WPF
csharpwpfquestionwcfdata-structures
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    esjq
    wrote on last edited by
    #1

    Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:

    private void PopulateContactsListBox()
    {
    listBoxContacts.Items.Clear();
    listBoxContacts.ItemsSource = contactClient.GetContacts();
    }

    My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!

    A P E 3 Replies Last reply
    0
    • E esjq

      Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:

      private void PopulateContactsListBox()
      {
      listBoxContacts.Items.Clear();
      listBoxContacts.ItemsSource = contactClient.GetContacts();
      }

      My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!

      A Offline
      A Offline
      Adam Maras
      wrote on last edited by
      #2

      I'm not sure if this is the same in WPF, but in ASP.NET the databound controls have properties for you to set which property or field of the returned data items are used for 1) displaying text and 2) the actual value of the item. Perhaps WPF has something similar?

      1 Reply Last reply
      0
      • E esjq

        Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:

        private void PopulateContactsListBox()
        {
        listBoxContacts.Items.Clear();
        listBoxContacts.ItemsSource = contactClient.GetContacts();
        }

        My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        You need to set the items that you want to display. Typically it would look something like this:

        <ListBox.ItemTemplate>
        <DataTemplate>
        <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=Title}" />
        <TextBlock Text="{Binding Path=Surname}" />
        </StackPanel>
        </DataTemplate>
        </ListBox.ItemTemplate>

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        C 1 Reply Last reply
        0
        • P Pete OHanlon

          You need to set the items that you want to display. Typically it would look something like this:

          <ListBox.ItemTemplate>
          <DataTemplate>
          <StackPanel Orientation="Horizontal">
          <TextBlock Text="{Binding Path=Title}" />
          <TextBlock Text="{Binding Path=Surname}" />
          </StackPanel>
          </DataTemplate>
          </ListBox.ItemTemplate>

          Deja View - the feeling that you've seen this post before.

          My blog | My articles | MoXAML PowerToys

          C Offline
          C Offline
          ColinM123
          wrote on last edited by
          #4

          Yeah, as Pete indicated you need a datatemplate as the listbox doesn't know how to display a Friend object so it just calls the ToString method. You can move the datatemplate into a resource section. Such as Window.Resources or even Application.Resources. Another neat thing is if you set the DataType on the DataTemplate to your Friend object, it will be used automatically display it. Along the lines of this: ....

          1 Reply Last reply
          0
          • E esjq

            Hi all! I've found lots of examples describing databinding, but still I haven't got a clue how to solve my problem. The frustrating thing is I think it is easy. But still, I'm lost... In a WPF application, I invoke a WCF service call (contactClient.GetContacts()), which returns an array of Friend objects. Window.xaml.cs:

            private void PopulateContactsListBox()
            {
            listBoxContacts.Items.Clear();
            listBoxContacts.ItemsSource = contactClient.GetContacts();
            }

            My intention is to let a listbox display the result returned from the WCF service call. And it does, but it doesn't display the actual data. Instead it displays the ToString() representation. In other words, the listbox displays the type returned by the service call; like "DataLayer.Friend". So, how do I display the actual data from the service call in my listbox? Thank you!

            E Offline
            E Offline
            esjq
            wrote on last edited by
            #5

            Thank you guys! It worked with the datatemplate stuff. Have a nice day :-)

            P 1 Reply Last reply
            0
            • E esjq

              Thank you guys! It worked with the datatemplate stuff. Have a nice day :-)

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You're welcome, and you have a nice day too. :-D

              Deja View - the feeling that you've seen this post before.

              My blog | My articles | MoXAML PowerToys

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups