Ankit
Ankit

Reputation: 6622

data not coming from ItemsSource

this is where I am trying to populate items in xaml

<ScrollViewer Grid.Row="2">
                <StackPanel>
                    <ItemsControl ItemsSource="{Binding EventsList}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Value}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </ScrollViewer>

this is a dummy method in view model, that I have Binded

public string[] EventsList()
        {
            string[] values = {"event1", "event2"};
            return values;
        }

but this isn't giving any output. also this method is not being called as well.

Upvotes: 1

Views: 47

Answers (3)

Ajay
Ajay

Reputation: 6590

Try this

MainPage.xaml
      <ScrollViewer Grid.Row="2">
            <StackPanel>
                <ItemsControl Name="itemControl" ItemsSource="{Binding EventsList}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </ScrollViewer>

MainPage.xaml.cs

 public MainPage()
    {
        InitializeComponent();            

        this.itemControl.ItemSource = EventsList();
    }

Upvotes: 0

Dutts
Dutts

Reputation: 6191

This isn't working because you are trying to bind to a method. You can only bind to properties.

public string[] EventsList
{
    get
    {
        string[] values = {"event1", "event2"};
        return values;
    }
}

Upvotes: 1

Kevin Gosse
Kevin Gosse

Reputation: 39007

Many issues here.

The first one is that you can't bind to a method. You can bind only to a property.

The second one is that you're binding the TextBlock to the Value of your object, which is supposed to be a string. A string has no Value property.

Try this instead:

public string[] EventsList
{
    get
    {
        string[] values = {"event1", "event2"};
        return values;
    }
}

Then bind to this property and display the full string object (by using {Binding})

<ScrollViewer Grid.Row="2">
    <StackPanel>
        <ItemsControl ItemsSource="{Binding EventsList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</ScrollViewer>

Note: it supposes that the class in which you're declaring the EventList property has been assigned to the DataContext property of your page.

Upvotes: 1

Related Questions