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

    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">
            <Grid>
                <TextBlock Text="{Binding TestString}" />
            </Grid>
    </DataTemplate>
    
    <!-- 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;
    	items->Append(item);
    }
    this->variationsListView->ItemsSource = items;
    
    
    // The item class
    
    public ref class MyTestItem sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    private:
    	void RaisePropertyChanged(Platform::String^ name);
    	Platform::String^ m_testString;
    
    public:
    	MyTestItem(void);
    	~MyTestItem(void);
    
    	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

Answers

  • 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

    http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5/

    The  team class.

     

    Best regards,

    Jesse


    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