locked
How to bind ListView to Platform::Collections::Map

    Question

  • I have a class with a Platform::Collection::Map data member. The class implements INotifyPropertyChanged and I have added an IObservableMap property for the Map. When I try to bind this property to a ListView control, nothing happens. 

    The property code looks like:

    property IObservableMap<String^, String^>^ StringMap
    {
    	IObservableMap<String^, String^>^ get()
    	{
    		return m_stringMap;
    	}
    
    	void set(IObservableMap<String^, String^>^ value)
    	{
    		m_stringMap = safe_cast<Map<String^, String^>^>(value);
    		OnPropertyChanged("StringMap");
    	}
    }


    And the ListView XAML is

    <ListView ItemsSource="{Binding Path=StringMap}">
        <ListView.ItemTemplate>
            <DataTemplate>        
                <TextBox Text="test"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    This setup doesn't work. Why not? 

    Thanks,

    David







    • Edited by davidb353 Thursday, November 28, 2013 8:25 PM
    Thursday, November 28, 2013 6:34 AM

Answers

  • Hi davidb353,

    I have tried many method but I still couldn't bind a map to a listview. And I think it is impossible to bind them together.

    Every time I try to bind them and I get an error that the map couldn't be the source of a listview. I think we also have no reason to do so, just store the value to a vector and bind the vector to the listview is more suitable.

    Sorry for my careless again, and in the grid app, it used the map only for store data.

    Below is my sample code:

    Map<String^, String^>^ mymap = ref new Map<String^, String^>();
    	mymap->Insert(ref new String(TEXT("TTT1")), ref new String(TEXT("bbb1")));
    	mymap->Insert(ref new String(TEXT("TTT2")), ref new String(TEXT("bbb2")));
    	mymap->Insert(ref new String(TEXT("TTT3")), ref new String(TEXT("bbb3")));
    	mymap->Insert(ref new String(TEXT("TTT4")), ref new String(TEXT("bbb4")));
    	MySource^ ms = ref new MySource();
    	ms->MyData = mymap;
    	Vector<String^>^ vector = ref new Vector<String^>();
    	for each (auto var in ms->MyData)
    	{
    		vector->Append(var->Value);
    	}
    	MyList->ItemsSource = vector;

    #include<collection.h>
    namespace My_Source{
    	public ref class MySource sealed{
    	public:
    		property Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::String^>^ MyData{
    			Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::String^>^ get(){
    				return _mydata;
    			}
    			void set(Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::String^>^ value){
    				_mydata = dynamic_cast<Platform::Collections::Map<Platform::String^, Platform::String^>^>(value);
    			}
    		};
    	private:
    		Platform::Collections::Map<Platform::String^, Platform::String^>^ _mydata;
    	};
    }

    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.

    • Marked as answer by davidb353 Tuesday, December 10, 2013 3:05 AM
    Tuesday, December 10, 2013 2:51 AM
    Moderator

All replies

  • Hi davidb353,

    Welcome here!

    Have you set the class as bindable?

    Maybe, you could build a Grid app, and look into NavigationHelper.h file which is within Common folder.

    There is a similar class you could refer.

    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.

    Friday, November 29, 2013 10:00 AM
    Moderator
  • I couldn't find a similar case in the Grid app. No map is being bound to a xaml control.
    • Edited by davidb353 Friday, November 29, 2013 9:11 PM
    Friday, November 29, 2013 9:11 PM
  • Hi davidb353,

    I mean this:


    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.

    Friday, December 6, 2013 9:17 AM
    Moderator
  • Is there any way to bind a Platform::Collections::Map to a ListView control? I can do it using a Vector but cannot make it work with a Map. Please help.
    Sunday, December 8, 2013 2:35 AM
  • The MFC project template does not even use a Platform::Collections::Map. This answer was not helpful at all.
    Sunday, December 8, 2013 2:39 AM
  • Hi davidb353,

    Sorry for my careless, I paste the wrong image. On above image, you could also find the grid app on #6 item of the list. It is here:

     


    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, December 9, 2013 3:13 AM
    Moderator
  • Hi davidb353,

    I have tried many method but I still couldn't bind a map to a listview. And I think it is impossible to bind them together.

    Every time I try to bind them and I get an error that the map couldn't be the source of a listview. I think we also have no reason to do so, just store the value to a vector and bind the vector to the listview is more suitable.

    Sorry for my careless again, and in the grid app, it used the map only for store data.

    Below is my sample code:

    Map<String^, String^>^ mymap = ref new Map<String^, String^>();
    	mymap->Insert(ref new String(TEXT("TTT1")), ref new String(TEXT("bbb1")));
    	mymap->Insert(ref new String(TEXT("TTT2")), ref new String(TEXT("bbb2")));
    	mymap->Insert(ref new String(TEXT("TTT3")), ref new String(TEXT("bbb3")));
    	mymap->Insert(ref new String(TEXT("TTT4")), ref new String(TEXT("bbb4")));
    	MySource^ ms = ref new MySource();
    	ms->MyData = mymap;
    	Vector<String^>^ vector = ref new Vector<String^>();
    	for each (auto var in ms->MyData)
    	{
    		vector->Append(var->Value);
    	}
    	MyList->ItemsSource = vector;

    #include<collection.h>
    namespace My_Source{
    	public ref class MySource sealed{
    	public:
    		property Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::String^>^ MyData{
    			Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::String^>^ get(){
    				return _mydata;
    			}
    			void set(Windows::Foundation::Collections::IObservableMap<Platform::String^, Platform::String^>^ value){
    				_mydata = dynamic_cast<Platform::Collections::Map<Platform::String^, Platform::String^>^>(value);
    			}
    		};
    	private:
    		Platform::Collections::Map<Platform::String^, Platform::String^>^ _mydata;
    	};
    }

    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.

    • Marked as answer by davidb353 Tuesday, December 10, 2013 3:05 AM
    Tuesday, December 10, 2013 2:51 AM
    Moderator
  • Okay, I guess that settles it. I will use a Vector work around but there are definitely use cases where binding to a Map would be very useful. Maybe you guys at MSFT can do something about that.

    Thanks,

    David


    • Edited by davidb353 Tuesday, December 10, 2013 3:05 AM
    Tuesday, December 10, 2013 3:05 AM