Click here to Skip to main content
15,885,949 members
Articles / Desktop Programming / XAML

Dynamic Silverlight controls binding with XML data using LINQ

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
9 Mar 2009CPOL2 min read 41.6K   13  
How to create dynamic Silverlight controls with animation binding with XML data.
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 System.Windows.Browser;
using System.Xml.Linq;
using System.Windows.Media.Imaging;

namespace BindingXML
{
  public partial class Page : UserControl
  {
    public Page()
    {
      InitializeComponent();

      LoadXMLFile();
    }

    private void LoadXMLFile()
    {
      WebClient xmlClient = new WebClient();
      xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
      xmlClient.DownloadStringAsync(new Uri("MainPageLinks.xml", UriKind.RelativeOrAbsolute));
    }

    void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
    {
      double initialTextLeft = 25;
      double initialImageLeft = 10;
      double initialTop = 40;

      Thickness thickButton = new Thickness();
      thickButton.Left = 8;
      thickButton.Top = -22;
      thickButton.Right = 143;
      thickButton.Bottom = 0;


      Point pointImageTransformOrigin = new Point();
      pointImageTransformOrigin.X = 0.5;
      pointImageTransformOrigin.Y = 0.5;

      if (e.Error == null)
      {
        string xmlData = e.Result;
        XDocument xdoc = XDocument.Parse(xmlData);

        var linkItems = from b in xdoc.Descendants()
                        where b.Name == "Link"
                        select b;

        foreach (XElement link in linkItems)
        {
          string linkText = link.Element("Text").Value;
          string URL = link.Element("URL").Value;
          string No = link.Attribute("No").Value;

          Canvas sampleCanvas = new Canvas();

          Image sampleImage = new Image();
          sampleImage.Source = new BitmapImage(new Uri("pencil.jpg", UriKind.Relative));
          sampleImage.VerticalAlignment = VerticalAlignment.Bottom;
          sampleImage.SetValue(Canvas.LeftProperty, initialImageLeft);
          sampleImage.SetValue(Canvas.TopProperty, initialTop);
          sampleImage.SetValue(NameProperty, "Image" + No);
          sampleImage.RenderTransformOrigin = pointImageTransformOrigin;

          TransformGroup imgTransGroup = new TransformGroup();
          ScaleTransform imgScale= new ScaleTransform();
          RotateTransform imgRotate = new RotateTransform();
          TranslateTransform imgTranslate = new TranslateTransform();
          SkewTransform imgScew = new SkewTransform();

          imgTransGroup.Children.Add(imgScale);
          imgTransGroup.Children.Add(imgRotate);
          imgTransGroup.Children.Add(imgTranslate);
          imgTransGroup.Children.Add(imgScew);

          sampleImage.RenderTransform = imgTransGroup;

          TextBlock sampleText = new TextBlock();
          sampleText.Text = linkText;
          sampleText.SetValue(Canvas.LeftProperty, initialTextLeft);
          sampleText.SetValue(Canvas.TopProperty, initialTop);

          sampleCanvas.Children.Add(sampleImage);
          sampleCanvas.Children.Add(sampleText);

          HyperlinkButton sampleButton = new HyperlinkButton();
          sampleButton.TargetName = "_blank";
          sampleButton.NavigateUri = new Uri(URL);
          sampleButton.VerticalAlignment = VerticalAlignment.Top;
          sampleButton.Foreground = new System.Windows.Media.SolidColorBrush(Colors.Black);
          sampleButton.MouseEnter += new MouseEventHandler(sampleButton_MouseEnter);
          sampleButton.MouseLeave += new MouseEventHandler(sampleButton_MouseLeave);
          sampleButton.SetValue(NameProperty, "Button" + No);
          sampleButton.Margin = thickButton;

          sampleButton.SetValue(Canvas.LeftProperty, initialTextLeft);
          sampleButton.SetValue(Canvas.TopProperty, initialTop);
          initialTop += 20;

          sampleButton.Content = sampleCanvas;
          this.LayoutRoot.Children.Add(sampleButton);
        } 
      }
    }

    private void sampleButton_MouseLeave(object sender, MouseEventArgs e)
    {
      HyperlinkButton linkControl = new HyperlinkButton();
      linkControl = (HyperlinkButton)sender;
      linkControl.FontWeight = FontWeights.Normal;
      Storyboard1.Stop();
    }

    private void sampleButton_MouseEnter(object sender, MouseEventArgs e)
    {
      String controlNo = "0";
      
      HyperlinkButton linkControl = new HyperlinkButton();
      linkControl = (HyperlinkButton)sender;
      linkControl.FontWeight = FontWeights.Bold;
      controlNo = linkControl.Name.Substring(6, linkControl.Name.Length - 6);

      samplePointAnim.SetValue(Storyboard.TargetNameProperty, "Image" + controlNo);
      sampleDoubleAnim1.SetValue(Storyboard.TargetNameProperty, "Image" + controlNo);
      sampleDoubleAnim2.SetValue(Storyboard.TargetNameProperty, "Image" + controlNo);

      Storyboard1.Begin();
    } 
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions