Why my ListBox is not displaying any data?

2019-09-01 14:26发布

Here is my XAML, in windows phone, I am setting the data using MVVM to the page, but i cannot find any data on the listbox.

<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>

And my 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();
            }
        }

Here I am setting the viewmodel to mainPage,

public MainPage()
        {
            InitializeComponent();

            this.DataContext = App.ViewModel;

        }

but i cannot find any detail on Page!, what could be the reason?

1条回答
劫难
2楼-- · 2019-09-01 15:01

You have not assigned any values to the ItemsSource Property of your Listbox,

In your XAML,

<ListBox Grid.Row="0" ItemsSource="{Binding Employees}" 
                     SelectionMode="Single" SelectedItem="{Binding CurrentSelectedEmployee, Mode=TwoWay}">
查看更多
登录 后发表回答