2011-11-25 21 views
1

我需要在datagrid中顯示我的數據。我使用下面的代碼:在WP7中使用DataGrid

前端:

命名空間:xmlns:datagrid="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

<datagrid:DataGrid Name="tgrid" AutoGenerateColumns="False" Grid.Row="2" HeadersVisibility="Column"> 
      <datagrid:DataGrid.Columns> 
       <datagrid:DataGridTextColumn Header="Name"> 

       </datagrid:DataGridTextColumn> 
      </datagrid:DataGrid.Columns> 
     </datagrid:DataGrid> 

後端:

 List<LItem> str = new List<LItem>(); 
     str.Add(new LItem() { Str = "chandra" }); 
     str.Add(new LItem() { Str = "chandra" }); 
     str.Add(new LItem() { Str = "chandra" }); 
     str.Add(new LItem() { Str = "chandra" }); 
     str.Add(new LItem() { Str = "chandra" }); 
     str.Add(new LItem() { Str = "chandra" }); 
     tgrid.ItemsSource = str; 

    public class LItem 
    { 
     private string _str; 

     public string Str 
     { 
      get { return _str; } 
      set { _str = value; } 
     } 
    } 

當我使用此代碼我得到一個例外,因爲:

The type 'projectName.MainPage', specified in the x:Class of '/MainPage.xaml' could not be found in any loaded assembly. 

回答

2

您顯示的異常似乎與某些未顯示的XAML有關。附加屬性x:Class指示用戶控件的代碼隱藏類。 projectName.MainPage類型是否在您的項目中存在?

我建議重新開始一個新的用戶控件,減慢添加代碼的速度,重新編譯並在緩慢添加代碼時運行。

+0

projectName.MainPage存在於我的項目中。當我評論datagrid部分時,程序照常執行。 –