Skip to main content Skip to footer

How To: Host Silverlight Listbox Control in Spread Cell

Spread for Silverlight does not have inbuilt feature to set the cell type for cells like its other versions (Asp.net and Windows Forms). But you can still put a Silverlight control in a cell using the CustomDrawingObject Class. In this example I will show how you can put a ListBox control in Spread cell using Custom Drawing Object API. Have a look at the code used:


public partial class MainPage : UserControl  
{  
   public MainPage()  
   {  
      InitializeComponent();  
      this.gcSpreadSheet1.Sheets.Clear();  
      this.gcSpreadSheet1.Sheets.Add(new MyWorksheet());  
      var sheet = this.gcSpreadSheet1.ActiveSheet;  
   }  
}  

public class ControlDrawingObject : CustomDrawingObject  
{  
   private Control _rootElement;  
   public ControlDrawingObject(int row, int col, Control control) : base(row, col) { _rootElement = control; this.ShowDrawingObjectOnly = true; }  

   public override FrameworkElement RootElement  
   {  
      get { \_rootElement.Margin = new Thickness(1); return \_rootElement; }  
   }  
}  

public class MyWorksheet : Worksheet  
{  
   public bool DrawingObjectVisible { get; set; }  
   public override DrawingObject[] GetDrawingObject(int row, int column, int rowCount, int columnCount)  
   {  
      if (row != 0 || column != 0) return base.GetDrawingObject(row, column, rowCount,  columnCount);  
      DrawingObject drawobj;  

      ListBox mylist = new ListBox();  
      mylist.ItemsSource = new String[] { "One", "Two", "Three", "Four" };  
      mylist.SelectionMode = SelectionMode.Single;  
      drawobj = new ControlDrawingObject(row, column, mylist);  
      return new DrawingObject[] { drawobj };  
   }  
}  

Refer to the given samples for complete implementation. Download VB Sample Download C# Sample

MESCIUS inc.

comments powered by Disqus