显示文本块中的列表数据与datagrid,sharepoint webpart

时间:2011-03-04 20:59:21

标签: sharepoint-2010 web-parts

我正在构建一个silverlight 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 个答案:

答案 0 :(得分:1)

要在UI线程中运行代码,请使用以下代码:

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