Saturday, September 18, 2010

Find control inside silverlight container controls

There is no standard API for finding a control inside a Silverlight page or container control like we have in ASP.NET. You need to traverse the container element using VisualTreeHelper. Here is a generic function that can find any type of control in side a container control using recursion.

public T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement
        {
           
            if (parent == null) return null;

            if (parent.GetType() == targetType && ((T)parent).Name == ControlName)
            {
                return (T)parent;
            }
            T result = null;
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);

                if (FindControl<T>(child, targetType, ControlName) != null)
                {
                    result = FindControl<T>(child, targetType, ControlName);
                    break;
                }
            }
            return result;
        }

parent: is the container control that needs to be traversed for the desired control.
targetType: Target Control type.
ControlName: is the name/Id of the control to be searched.

Example Usage:
TextBlock ControlToSearch = FindControl<TextBlock>((UIElement)ContainerControl, typeof(TextBlock), "NameOfTheControlToSearch");

Method will return the desired control if found in container otherwise NULL.

Sunday, September 5, 2010

Export Silverlight charts to MS word

Writing MS word file with Silverlight needs out of browser functionality but you can avoid this by creating a word file on server and then send this created file back to client using a HTTPhandler. This sample word export application is created using the second approach.
I have created an object oriented design for this component as I will soon extend this to a complete library for exporting Silverlight UI (chart) to DOC, XLS,PDF and other image formats.
Using the code:
  1. Place “MSWordExportHandler.ashx” and “MSWordExportHandler.cs” clientweb folder of the web application hosting your Silverlight application.
  2. Add reference to “Microsoft.Office.Interop.Excel.dll” and “Microsoft.Office.Interop.Word.dll” in you server application (web application) hosting your Silverlight application.
  3. Add reference of the component to your Silverlight project.
  4. Create an object of Export Fa├žade class in your Silverlight page like the following
  5. ExportFacade Exporter = new ExportFacade("DOC (*.Doc)|*.doc");
  6. Export your UIelement i.e Silverlight chart by calling the Exporter.Export(UIElement Chart);
  7. ExportFacade class expose two event that you optionally can capture “ExportStarted” and “ExportCompleted”
Complete snippet of the click event of export button  

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            ExportFacade Exporter = new ExportFacade("DOC (*.Doc)|*.doc");
            Exporter.ExportStarted += new ExportFacade.ExportStartedDelegate(Exporter_ExportStarted);
            Exporter.ExportCompleted += new ExportFacade.ExportCompletedDelegate(Exporter_ExportCompleted);
            Exporter.Export(crtChart);
        }


Sample Code