这是我的XAML,在Windows手机中,我使用MVVM将数据设置到页面,但我在列表框中找不到任何数据。
<ListBox Grid.Row="0" SelectionMode="Single" SelectedItem="{Binding CurrentSelectedEmployee, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"></TextBlock>
<TextBlock Width="5"></TextBlock>
<TextBlock Text="{Binding LastName}"></TextBlock>
</StackPanel>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的ViewModel,
public MainViewModel()
{
Employees = new ObservableCollection<Model.Employee>();
}
public bool IsLoaded { get; set; }
public void LoadData()
{
Employees = CompanyDataPhone.Service.EmployeeService.GetEmployees();
IsLoaded = true;
}
// public ObservableCollection<Model.Employee> Employees { get; set; }
public ObservableCollection<CompanyDataPhone.Model.Employee> _employees;
public ObservableCollection<CompanyDataPhone.Model.Employee> Employees
{
get {
return _employees;
}
set
{
_employees = value;
OnpropertyChanged();
}
}
这里我将viewmodel设置为mainPage,
public MainPage()
{
InitializeComponent();
this.DataContext = App.ViewModel;
}
但我在页面上找不到任何细节!可能是什么原因?
答案 0 :(得分:5)
您尚未为列表框的ItemsSource属性分配任何值,
在你的XAML中,
<ListBox Grid.Row="0" ItemsSource="{Binding Employees}"
SelectionMode="Single" SelectedItem="{Binding CurrentSelectedEmployee, Mode=TwoWay}">