为什么ListView没有填充DataSource?

时间:2013-06-04 10:02:59

标签: c# xaml windows-store-apps

我正在制作Windows应用商店应用。 在XAML主页面中,我有一个ListView,我试图将它绑定到代码隐藏文件中的observable集合。

但我看到的是一个空白屏幕。 为什么列表没有填充?

修改

我正在创建一个应用程序来显示XAML中某些UI控件的名称,稍后我将在列表的每一行上添加单击事件,转到显示该控件的演示的新页面,这就是我使用该名称的原因控制模型。我正在制作我自己的简单厨房水槽应用程序。

以下是 MainPage.xaml

<ListView x:Name="listOfControls" ItemsSource="{Binding controlsDataSource}" SelectionChanged="listOfControls_SelectionChanged">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding name}"></TextBlock>
                <TextBlock Text="{Binding desc}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

以下是 MainPage.xaml.cs

注意:代码中使用的术语Control是我给一个与UI控件无关的模型类的名称。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace AllControls
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private ObservableCollection<AllControls.Models.Control> controlsDataSource { get; set; }
        public MainPage()
        {
            this.InitializeComponent();
            this.controlsDataSource = new ObservableCollection<AllControls.Models.Control>();
            this.controlsDataSource.Add(new AllControls.Models.Control("ScrollViewer", "ScrollViewer Desc"));
            this.controlsDataSource.Add(new AllControls.Models.Control("ScrollViewer", "ScrollViewer Desc"));
            this.controlsDataSource.Add(new AllControls.Models.Control("ScrollViewer", "ScrollViewer Desc"));
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        void listOfControls_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}

以下是模型, Control.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AllControls.Models
{
    class Control
    {
        private String name{ get;set;}
        private String desc { get; set; }
        public Control(String iName, String iDesc)
        {
            name = iName;
            desc = iDesc;

        }
    }

}

1 个答案:

答案 0 :(得分:0)

所以,我让它发挥作用。

在XAML中,

我只指定了ItemsSource="{Binding controlsDataSource}"

,而不是ItemsSource="{Binding}

然后在代码隐藏文件的MainPage构造函数中,我以编程方式将listView控件的DataContext设置为Array。

public MainPage()
        {
            this.InitializeComponent();
            this.controlsDataSource = new ObservableCollection<AllControls.Models.Control>();
            this.controlsDataSource.Add(new AllControls.Models.Control("ScrollViewer", "ScrollViewer Desc"));
            this.controlsDataSource.Add(new AllControls.Models.Control("ScrollViewer", "ScrollViewer Desc"));
            this.controlsDataSource.Add(new AllControls.Models.Control("ScrollViewer", "ScrollViewer Desc"));
            listOfControls.DataContext = controlsDataSource;      
  }

此外,我将名称和desc属性保留为私有,我公开,可能是需要的实际更改。 我认为私有会将变量设为私有,并设置并获取将告诉他们是否可以访问这些变量,但后来我意识到私人外部也使得setter也是私有的。

多数民众赞成。

这是完成我需要的另一种方法,但应该有一种方法在标记中指定dataSource列表。

欢迎更多答案,但在此之前,任何人都可以。