Subby
Subby

Reputation: 5480

Windows Phone Binding

In my RoomView.xaml I have:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox ItemsSource="{Binding myStrings, Mode=TwoWay}"></ListBox>
</Grid>

In my constructor I am doing:

 var myStrings = new List<string>{"Usmaan","Carl","Andy","Saul"};
 DataContext = myStrings;

Yet nothing is being spat out on the page when I load the application.

Can anyone see where I am going horribly wrong?

Upvotes: 0

Views: 62

Answers (1)

Olivier Payen
Olivier Payen

Reputation: 15268

The DataContext of your page is already set to the List object, so you just need to set the binding like this:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox ItemsSource="{Binding, Mode=TwoWay}"></ListBox>
</Grid>

Alternatively, you could create an object that has a MyStrings property and use it as the DataContext of the page. Then you could bind the ListBox like you did {Binding myStrings, Mode=TwoWay} while also being able to bind other controls to other properties of that object (that's the principle of ViewModels).

Upvotes: 1

Related Questions