locked
C++/CX ComboBox ItemsSource - Vector & Arrays

    Question

  • Hello

    I am a novice developer

    This is a trivial question though I am developing a Windows Store app and I'm having some difficulty in setting up the ItemsSource property of a Combobox. I am using a MVVM methodology in C++/CX with the Model coded in native C++.

    I tried setting up the data in the Model as an Enum, Array and now a Vector but I can't seem to get it working, I'm receiving different errors from unresolved symbols to can't cast overloaded functions.

    Can someone please help me.

    In short, I have a set of strings which I need to be the data that feeds into the combobox. I would like it setup in this fashion.

    MODEL - Contains native C++ and holds the data that feeds into the combobox

    VIEW-MODEL - Binds the data to the combobox in the model

    MODEL Allows the user to select a value which then raises an event back to the view-model

    Any help would be much appreciated

    Thank you

    Monday, September 23, 2013 3:41 AM

Answers

  • Some code samples and error messages would be helpful. In a situation like this I usually define a custom class with a property of the string value I want to display and give the class the Bindable attribute. I then define a Vector<myclass>, fill it with my custom class objects and assign the vector to the combobox ItemsSource. In XAML I use an Itemtemplate with a TextBlock control and bind the class property to the Text of the TextBlock control.
    Monday, September 23, 2013 1:25 PM
  • Hi YLD_1,

    Welcome to MSDN forum!

    According to your description. I advise you to use IVector instead of Vector. The error you has got is exposed that you are using some types which is not supported on WINRT type system.

    Windows::Foundation::Collections::IVector<Platform::String^>^ App::get()
    {
    	Platform::Collections::IVector<Platform::String^>^ _financialYear =
    	ref new Platform::Collections::IVector<Platform::String^>();
    	return _financialYear;
    }

    And you could get more useful info on the article shown below:

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

    I hope it is helpful!

    Regards!


    <font color="0099FF">&lt;THE CONTENT IS PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED&gt;</font><br/> Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 27, 2013 8:04 AM
    Moderator

All replies

  • Some code samples and error messages would be helpful. In a situation like this I usually define a custom class with a property of the string value I want to display and give the class the Bindable attribute. I then define a Vector<myclass>, fill it with my custom class objects and assign the vector to the combobox ItemsSource. In XAML I use an Itemtemplate with a TextBlock control and bind the class property to the Text of the TextBlock control.
    Monday, September 23, 2013 1:25 PM
  • Hi there

    thanks for the quick reply

    Here is my raw data

    std::array<std::string, 3> array_FinancialYear =
    {
    "2011-2012",
    "2012-2013",
    "2013-2014",
    };

    std::vector<std::string> FinancialYear(array_FinancialYear.begin(), array_FinancialYear.end());

    I had to create an array then move it to a vector due to some limitations in VS C++ 2012

    I then have a function in my model which can be called to return the value

    std::vector<std::string> getFinancialYear()
    {
    return FinancialYear;
    }

    In the viewmodel I have created the Vector property (i've made some progress since my last post).

    All I need to know is how I call the function "getFinancialYear()" and insert the data into the variable _financialYear. 

    Platform::Collections::Vector<Platform::String^>^ get()
    {
    Platform::Collections::Vector<Platform::String^>^ _financialYear =
    ref new Platform::Collections::Vector<Platform::String^>();
    return _financialYear;
    }

    Help is much appreciated

    Thursday, September 26, 2013 1:15 AM
  • If you're using this data to populate a ComboBox then I would create a Platform::Collections::Vector<Platform::String> object and use it as the ItemsSource of your ComboBox. The current selected string could then be accessed through the SelectedItem property of the ComboBox in either an event handler or other location. Of course you would need to check for nullptr if there was no current selection in the ComboBox.
    Thursday, September 26, 2013 2:45 PM
  • Hi

    I've created this object I'm just having some trivial issues on bringing the raw STD::VECTOR data over to the object

    see below, this is what is in my viewmodel. Its a novice question I know but I just dont know how to get it working. Thanks

    Platform::Collections::Vector<Platform::String^>^ get()
    {
    Platform::Collections::Vector<Platform::String^>^ _financialYear =
    ref new Platform::Collections::Vector<Platform::String^>();
    return _financialYear;
    }

    Friday, September 27, 2013 3:10 AM
  • Hi YLD_1,

    Welcome to MSDN forum!

    According to your description. I advise you to use IVector instead of Vector. The error you has got is exposed that you are using some types which is not supported on WINRT type system.

    Windows::Foundation::Collections::IVector<Platform::String^>^ App::get()
    {
    	Platform::Collections::IVector<Platform::String^>^ _financialYear =
    	ref new Platform::Collections::IVector<Platform::String^>();
    	return _financialYear;
    }

    And you could get more useful info on the article shown below:

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

    I hope it is helpful!

    Regards!


    <font color="0099FF">&lt;THE CONTENT IS PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED&gt;</font><br/> Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 27, 2013 8:04 AM
    Moderator
  • Hi

    I've created this object I'm just having some trivial issues on bringing the raw STD::VECTOR data over to the object

    see below, this is what is in my viewmodel. Its a novice question I know but I just dont know how to get it working. Thanks

    Platform::Collections::Vector<Platform::String^>^ get()
    {
    Platform::Collections::Vector<Platform::String^>^ _financialYear =
    ref new Platform::Collections::Vector<Platform::String^>();
    return _financialYear;
    }


    I'm not quite clear on what you are trying to accomplish. If you are wanting to provide a bound datasource for a ComboBox then you can instantiate a Vector class, fill it with values and bind it to the ComboBox::ItemsSource. If you are trying provide a Vector as a property to a class then you need to do as Xiaoliang suggested and expose the property as an IVector interface. If you are trying to expose a property that is the selected item of the ComboBox then in your get() of the property you can access the SelectedItem of the ComboBox and return it.
    Friday, September 27, 2013 2:25 PM