Binding and DataTemplate for ListView not working in C++ (but in C#) RRS feed

  • Question

  • Hi,

    while I'm able to get this to work under C# I am not able to do this in C++. I have a ListView with a custom ItemsSource consisting of a list of custom objects. It seems I cannot apply a dataTemplate. So here is the XAML code:

    <!-- In style dictionary -->
    <DataTemplate x:Key="MyVariationTemplate">
                <TextBlock Text="{Binding TestString}" />
    <!-- In actual page -->
    <ListView x:Name="variationsListView" ItemTemplate="{StaticResource MyVariationTemplate}" />

    But I goes the problem is in my C++ code since I get it to work in C#. Or is it a bug or did I miss something?

    The problem is that when I set the dataTemplate the items are just shown as empty (instead of the desired string).

    C++ Code:

    // Code for adding items and setting ItemsSource
    Windows::Foundation::Collections::IObservableVector<Object^>^ items = ref new Platform::Collections::Vector<Object^>();
    for (int i = 0; i < 10; ++i)
    	MyTestItem^ item = ref new MyTestItem();
    	item->TestString = "itemX " + i;
    this->variationsListView->ItemsSource = items;
    // The item class
    public ref class MyTestItem sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    	void RaisePropertyChanged(Platform::String^ name);
    	Platform::String^ m_testString;
    	event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;
    	property Platform::String^ TestString
    		Platform::String^ get() { return m_testString; }
    		void set(Platform::String^ value) { m_testString = value; RaisePropertyChanged("TestString"); }

    • Edited by peterGro Tuesday, May 8, 2012 7:56 AM updated title
    Tuesday, May 8, 2012 7:40 AM


  • Hello,


    There should be another things you need to add into your codes.


    First, there should be a item class inherit from Imap and Iiterable. And the item class should have [Windows::UI::Xaml::Data::Bindable] attribute.


    There is a sample codes for databinding, Please check this


    The  team class.


    Best regards,


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by peterGro Wednesday, May 9, 2012 10:43 AM
    Wednesday, May 9, 2012 10:36 AM