2011-03-04 47 views
1

我正在構建Silverlight Web部件。我只是想在文本塊中顯示sharepoint列表數據與數據網格,因爲我只打算從列表中返回一個項目。我設法得到我想要的結果在一個數據網格,但我不知道如何修改我的代碼,所以我可以顯示我的數據在文本塊。在textblock verses datagrid中顯示列表數據,sharepoint webpart

我以爲我可以簡單的寫

texblock1.text = projects; 

但它拋出一個錯誤。

這裏的代碼隱藏在我的XAML主頁-------------------

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.SharePoint.Client; 

namespace something{   

    public class Project{   
     public string Title {get; set;}   
    }  

    public partial class MainPage : UserControl  
    {   
     public string SiteUrl { get; set; }   

     private ListItemCollection _projects;   

     //private Web _web = null;   
     //private string _lastErrorMessage = null;   

     public MainPage()   
     {    

      InitializeComponent(); 
      ClientContext context = new ClientContext(ApplicationContext.Current.Url); 
      context.Load(context.Web); 
      List Projects = context.Web.Lists.GetByTitle("projects"); 
      context.Load(Projects); 
      CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery(); 

      string camlQueryXml = "<View><Query><Where><Eq><FieldRef Name=\"NameLast\" /><Value Type=\"Boolean\">1</Value></Eq></Where></Query></View>"; 

      query.ViewXml = camlQueryXml; 
      _projects = Projects.GetItems(query); 
      context.Load(_projects);context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);   

     }   

     private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)   
     {    

      // This is not called on the UI thread.    
      Dispatcher.BeginInvoke(BindData);   
     }   

     private void BindData()   
     {    

      List<Project> projects = new List<Project>();    

      foreach (ListItem li in _projects)    

      {     

       projects.Add(new Project()     

       {      
        Title = li["Title"].ToString(), 
       });    

      }    

      dataGrid1.ItemsSource = projects; // must be on UI thread   
     }  
    } 
} 

回答

1

要在UI線程使用的代碼運行代碼如下:

Dispatcher.BeginInvoke(() => { 
    //add code here to which are to be executed on the UI thread 
}); 
+0

多一點指導,將不勝感激!感謝您的輸入。 – 2011-03-06 22:08:08

相關問題