locked
VS2013 RC: can't bind to Size property of a Vector

    Question

  • I'm unable to bind to the Size property of a Vector<String^>. A very similar example is working in C#. I'm attempting this in VS2013 RC. I haven't tried older versions.

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                names = new List<String>();
                names.Add("m 1");
                names.Add("m 2");
                this.DataContext = names;
            }
    
            private List<String> names;
        }
    
    
    <Page
        x:Class="InitCSharp.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:InitCSharp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <ListView
                x:Name="People" Grid.Column="0" Margin="20"
                ItemsSource="{Binding}">
    
            </ListView>
    
            <Grid Grid.Column="1" Margin="20">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <TextBlock
                    x:Name="Name" Grid.Row="0" Margin="0,30"
                    Style="{ThemeResource HeaderTextBlockStyle}"
                    Text="{Binding ElementName=People, Path=SelectedItem, Mode=OneWay, FallbackValue=c#fallback0}"/>
                <TextBlock
                    x:Name="Idx" Grid.Row="1" Margin="20,0,0,0"
                    Style="{ThemeResource SubheaderTextBlockStyle}"
                    Text="{Binding ElementName=People, Path=SelectedIndex, Mode=OneWay, FallbackValue=c#fallback1}"/>
                <TextBlock
                    x:Name="Count" Grid.Row="3" Margin="0"
                    Style="{ThemeResource SubheaderTextBlockStyle}"
                    Text="{Binding Path=Count, Mode=OneWay, FallbackValue=c#fallback3}"/>
            </Grid>
        </Grid>
    </Page>
    

    The above looks to be working (though maybe not for the right reasons). The below code is what fails.

    //
    // MainPage.xaml.h
    // Declaration of the MainPage class.
    //
    
    #pragma once
    
    #include "MainPage.g.h"
    #include <collection.h>
    
    namespace Initiative
    {
    	/// <summary>
    	/// An empty page that can be used on its own or navigated to within a Frame.
    	/// </summary>
    	public ref class MainPage sealed
    	{
    	public:
    		MainPage();
    	private:
    		Platform::Collections::Vector<Platform::String^>^ names;
    	};
    }
    
    
    //
    // MainPage.xaml.cpp
    // Implementation of the MainPage class.
    //
    
    #include "pch.h"
    #include "MainPage.xaml.h"
    
    using namespace Initiative;
    
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    MainPage::MainPage()
    {
    	InitializeComponent();
    	names = ref new Platform::Collections::Vector<String^>();
    	names->Append(ref new String("m 1"));
    	names->Append(ref new String("m 2"));
    	this->DataContext = names;
    	People->SelectedIndex = 0;
    }
    
    
    <Page
        x:Class="Initiative.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Initiative"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Page.Resources>
    
        </Page.Resources>
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <ListView
                x:Name="People" Grid.Column="0" Margin="20"
                ItemsSource="{Binding}">
    
            </ListView>
    
            <Grid Grid.Column="1" Margin="20">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <TextBlock
                    x:Name="Name" Grid.Row="0" Margin="0,30"
                    Style="{ThemeResource HeaderTextBlockStyle}"
                    Text="{Binding ElementName=People, Path=SelectedItem, Mode=OneWay, FallbackValue=fallback0}"/>
                <TextBlock
                    x:Name="Idx" Grid.Row="1" Margin="20,0,0,0"
                    Style="{ThemeResource SubheaderTextBlockStyle}"
                    Text="{Binding ElementName=People, Path=SelectedIndex, Mode=OneWay, FallbackValue=fallback1}"/>
                <TextBlock
                    x:Name="Count" Grid.Row="3" Margin="0"
                    Style="{ThemeResource SubheaderTextBlockStyle}"
                    Text="{Binding Path=Size, Mode=OneWay, FallbackValue=fallback3}"/>
            </Grid>
        </Grid>
    </Page>

    I tried using Count as well as Size for the binding path. 
    Monday, September 16, 2013 5:59 PM

All replies

  • What exactly is failing? What is the expected behavior to contrast with the actual behavior?

    --Rob

    Tuesday, September 17, 2013 5:42 PM
    Owner
  • The binding to the collection's Size property fails in the C++/CX example. The Vector is set as the page's DataContext. With the page being a parent of the TextBlock control, I thought Path=Size would find Vector->Size property.

    The C++/CX code feels like the same thing that I did in C#, which correctly bound to the Count property of the List that's set as the page's DataContext.

    This is the error I see running the C++/CX code:

    Error: BindingExpression path error: 'Size' property not found on 'Windows.Foundation.Collections.IObservableVector`1<Initiative.Entry>'. BindingExpression: Path='Size' DataItem='Windows.Foundation.Collections.IObservableVector`1<Initiative.Entry>'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')

    I suppose I'm not good at using the Object Browser. I don't know why I don't see any properties on the Windows::Foundation::Collections::IObservableVector<T>, only a VectorChanged event. 

    Wednesday, September 18, 2013 4:11 AM