I am building a simple windown phone 7 Page. I'm doing MVVM (using MVVM light) and binding a List<Category>
type property to ListPicker
. This property is defined in a view model named AddExpenseViewModel
like below
public class AddExpenseViewModel:ViewModelBase
{
public List<Category> Categories
{
get { return categories; }
set
{
categories = value;
RaisePropertyChanged("Categories");
}
}
}
Category
class is defined as
public class Category
{
public string Name { get; set; }
}
In my XAML I first define a resource as
<UserControl.Resources>
<bs:ViewModelLocator x:Key="ViewModelLocator" />
</UserControl.Resources>
Then set the DataContext
of the grid that contains the ListPicker
as
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="13,1,11,-1"
DataContext="{Binding Path=AddExpenseViewModel,
Source={StaticResource ViewModelLocator}}">
And here is my XAML for ListPicker
<toolkit:ListPicker
HorizontalAlignment="Left"
Height="50"
Width="200"
Grid.Row="2"
Grid.Column="1"
DataContext="{Binding AddExpenseViewModel}"
ItemsSource="{Binding Path=Categories, Mode=OneWay}" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="LightGreen" Width="*" Height="*">
<TextBlock Text="{Binding Name}"></TextBlock>
</Border>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>`
This does not work. The ListPicker
is always empty. Am I doing anything wrong here?