为什么我的 ListBox 没有显示任何数据? [英] Why my ListBox is not displaying any data?

查看:69
本文介绍了为什么我的 ListBox 没有显示任何数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 XAML,在 Windows Phone 中,我正在使用 MVVM 将数据设置到页面,但在列表框中找不到任何数据.

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>

还有我的 ViewModel,

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

这里我将 viewmodel 设置为 mainPage,

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?

推荐答案

您尚未为列表框的 ItemsSource 属性分配任何值,

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

在您的 XAML 中,

In your XAML,

<ListBox Grid.Row="0" ItemsSource="{Binding Employees}" 
                     SelectionMode="Single" SelectedItem="{Binding CurrentSelectedEmployee, Mode=TwoWay}">

这篇关于为什么我的 ListBox 没有显示任何数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆