Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, April 01, 2013

Binding items From SharePoint List to Combo Box using SilverLight Client Object Model

Note:-
If you want to bind both Value and Text for Combo Box Please select SilverLight Version 4 in order to make selected value property of Combo Box to work. In SilverLight Version3 you will not find the property of selected value for Combo Box.
Ascx
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <ComboBox Height="37" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ddlBindData" VerticalAlignment="Top" Width="247" DisplayMemberPath="Title" SelectedValuePath="ID" SelectedValue="{Binding Path=ID, Mode=TwoWay}"  />
    </Grid>
</UserControl>


Ascx.Cs
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 SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            ddlBindData.SelectionChanged += new SelectionChangedEventHandler(ddlBindData_SelectionChanged);
            getData();
        }

        void ddlBindData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show(ddlBindData.SelectedValue.ToString());
        }

        public class NewThoughts
        {
            public int ID{get;set;}
            public string Title{get;set;}
        }
        private IEnumerable<ListItem> myColl = null;
        public void getData()
        {
            try
            {
                ClientContext context = ClientContext.Current;
                Web objWeb = context.Web;
                List lstNewThoughts = objWeb.Lists.GetByTitle("New Ideas");
                CamlQuery strQuery = new CamlQuery();
                strQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='Title' /></OrderBy></Query></View>";
                var items = lstNewThoughts.GetItems(strQuery);
                context.Load(lstNewThoughts);
                myColl = context.LoadQuery(items.Include(l => l["Title"], l=>l.Id));
                context.ExecuteQueryAsync(OnListItemsLoadSucceeded, OnFailure);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
            }
        }
        private delegate void UpdateUIMethod();
        private void OnListItemsLoadSucceeded(object sender, ClientRequestSucceededEventArgs e)
        {
            UpdateUIMethod updateUI = LoadListItems;
            this.Dispatcher.BeginInvoke(updateUI);
        }
        private void OnFailure(object sender, ClientRequestFailedEventArgs e)
        {
            MessageBox.Show("Request Failed: " + e.Message + ", Stack Trace:" + e.StackTrace);
        }
        private void LoadListItems()
        {
            ddlBindData.Visibility = System.Windows.Visibility.Visible;
            List<NewThoughts> lItems = new List<NewThoughts>();
            foreach (ListItem item in myColl.ToList())
                lItems.Add(new NewThoughts
                {
                    Title = item["Title"].ToString(),
                    ID=item.Id,
                });
            ddlBindData.ItemsSource = lItems;
        }
    }

}


No comments:

Post a Comment