Why my ListBox is not displaying any data?

半世苍凉 提交于 2019-12-13 05:08:08

问题


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:


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


来源:https://stackoverflow.com/questions/22463195/why-my-listbox-is-not-displaying-any-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!