2013-06-04 104 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;  
    } 

而且我一直保持的名稱和說明性質爲私有,我公之於衆,可能是需要的實際變化。 我認爲私人會使變量變爲私人的,並且會告訴我們它們是否可以訪問,但是後來我意識到私人的外部設備也是私人的。

就是這樣。

這是完成我所需要的另一種方式,但應該有一種方法來指定標記中的數據源列表。

更多的答案是歡迎,但在那時任何人。