2013-06-04 108 views
1

我正在制作Windows应用商店应用。 在XAML主页面我有一个ListView,我试图将它绑定到代码隐藏文件后面的observable集合。为什么ListView不能用DataSource填充?

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

编辑:

我想提出一个应用程序显示在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中

注:所用的术语控制在代码中是我给的一个名字,与模型类没有任何关系一个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; 

     } 
    } 

} 
+0

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

+1

je kahi lihayachay问题babat,直接问题madhye lihit ja – Freelancer

+0

niruttar aahet sagale tumachya prashna pudhe !!! – Freelancer

回答

0

所以,我得到它的工作。

在XAML,

相反的ItemsSource="{Binding controlsDataSource}",我只指定了ItemsSource="{Binding}

,然后在的MainPage构造函数中的代码隐藏文件,我设置的ListView控件数组编程的DataContext的。

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

而且我一直保持的名称和说明性质为私有,我公之于众,可能是需要的实际变化。 我认为私人会使变量变为私人的,并且会告诉我们它们是否可以访问,但是后来我意识到私人的外部设备也是私人的。

就是这样。

这是完成我所需要的另一种方式,但应该有一种方法来指定标记中的数据源列表。

更多的答案是欢迎,但在那时任何人。