locked
C++ Listbox ItemsSource

    Question

  • I am normally a C# Dev. but I am trying to get in to C++ Dev.

    So, I am trying to send my JSON Response:

    element->GetObject()->GetNamedObject("profile")->GetNamedString("thumbnail");

    To go to a ListBox or ListView... In my XAML I am using: <Image Source="{Binding thumbnail}" /> for the image, but unfortunately I can't figure out how to get the List to accept the image/use the image from the JSON response.

    Even if I do:

    lstBox->Items.Append(element->GetObject()->GetNamedObject("profile")->GetNamedString("thumbnail"));

    This process only adds the thumbnail URL's to the Listbox as text.So, how can I get the JSON response to bind to my XAML?

    Thanks!



    Lance Seidman

    Monday, October 28, 2013 9:23 PM

Answers

All replies

  • Hi Lance Seidman,

    Welcome to MSDN forum!

    According to your description, I advise you to create a vector to collect data, and append it to listbox as source.

    Platform::Collections::Vector<String^>^ fonts = 
        ref new Platform::Collections::Vector<String^>();
    fonts->Append("Item 1");
    fonts->Append("Item 2");
    fonts->Append("Item 3");
    listbox1->ItemsSource = fonts;
    

    You could refer the below article:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh868195.aspx

    I hope it is helpful!

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 29, 2013 9:51 AM
    Moderator
  • Hi,

    Thanks for responding but my issue is, I can't get the image I have in the

    XAML to see the JSON input...

    Any idea why that would be?

    Even though I am using {Binding thumbnail} for the source...

    Thanks,

    Lance


    Lance Seidman

    Tuesday, October 29, 2013 2:34 PM
  • Hi Lance Seidman,

    Welcome back!

    I'm not sure how you handle your JSON Response data. Have you convert the data as a attribute of an object before you bind it to Image?

    Maybe the post shown below is helpful though it is for phone!

    http://stackoverflow.com/questions/18354604/unable-to-bind-json-data-to-list-in-windows-phone-8

    http://stackoverflow.com/questions/15965437/data-binding-to-listbox-from-json-response

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 30, 2013 2:41 AM
    Moderator
  • Hello,

     The issue with that, that's C# not C++. I am terrific at C#, just C++ seems to be a challenge.

    What am I doing? This:

    JsonValue^ jsonValue = JsonValue::Parse(response);        

    int arraySize = jsonValue->GetArray()->Size;        

    for (int i = 0; i < arraySize; i++)      

     {          

     IJsonValue^ element = jsonValue->GetArray()->GetAt(i);        

    }

    With that being said, to access the data I would do... But only adds the items received from JSON to the list, it doesn't bind to my XAML, which is why I need to do:

    lst->ItemsSource = element->GetObject()->GetNamedObject("profile")->GetNamedString("thumbnail");

    But that doesn't work, unfortunately. Even if I do:

    Platform::Collections::Vector<String^>^ theList = ref new Platform::Collections::Vector<String^>();

    theList->Append(element->GetObject()->GetNamedObject("profile")->GetNamedString("thumbnail"));

    lst->ItemsSource = theList;

    If this was C#, I would be done... :{

    Lance Seidman

    Wednesday, October 30, 2013 3:05 AM
  • Hi Lance Seidman,

    for (int i = 0; i < arraySize; i++)       
    
     {           
    
     IJsonValue^ element = jsonValue->GetArray()->GetAt(i);         
    
    }
    

    I don't know why you give the value to a temporary variable element. Maybe it's your issue situation?

    And theList collects the image uri? How do you set the Image element context?

    Would you mind to sharing the uri string example with me ?

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 30, 2013 8:06 AM
    Moderator
  • Hi,

     I am not sure what you mean? But let me explain my process...

    I'm trying to take a JSON Array, and bind its contents to a Listbox. I have Text & an Image that must be used from the JSON to the XAML.

    I know in C# I can just do var bar = JsonConvert.Deserialize<List<RootObject>>(response); Then use a for loop and put data to a list with ItemsSource.

    If you have ANY example that's idiot proof on how to parse a JSON response and send the response to a Lid


    Lance Seidman

    Wednesday, October 30, 2013 8:28 AM
  • Hi Lance Seidman,

    Welcome back!

    Before all, you need to install JSON.net first and add it to your project. If you are doubt on it, please see here.

    http://stackoverflow.com/questions/17165604/get-string-from-json-file-c-cli

    You could refer the post shown above.

    I hope it is helpful!

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 31, 2013 6:35 AM
    Moderator
  • Any chance you know of an alternative for Windows 8.1 Store App's? NewtonSoft JSON Control doesn't exist for it yet, I can't use the NuGet JSON in the project for 8.1 App's.

    Thanks!


    Lance Seidman

    Friday, November 1, 2013 10:45 PM
  • Hi Lance Seidman,

    Welcome here!

    I could not find the alternative for windows 8.1, I will report it and if possible you could get the update info from Microsoft.com.

    Thanks for your understanding!

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 4, 2013 6:08 AM
    Moderator
  • Have you tried looking at the C++ REST SDK? These links might help you (especially the second one): http://msdn.microsoft.com/library/jj969455(v=vs.120).aspx and http://msdn.microsoft.com/en-US/library/jj950082.aspx .


    Visual C++ MVP | Website | Blog | @mikebmcl | Windows Store DirectX Game Template

    Wednesday, November 6, 2013 3:15 PM