Click here to Skip to main content
11,435,451 members (53,178 online)
Click here to Skip to main content
Add your own
alternative version

Dynamic Silverlight controls binding with XML data using LINQ

, 9 Mar 2009 CPOL
How to create dynamic Silverlight controls with animation binding with XML data.
BindingXML.zip
BindingXML
BindingXML
Bin
Debug
BindingXML.dll
BindingXML.pdb
BindingXML.xap
de
System.Xml.Linq.resources.dll
es
System.Xml.Linq.resources.dll
fr
System.Xml.Linq.resources.dll
it
System.Xml.Linq.resources.dll
ja
System.Xml.Linq.resources.dll
ko
System.Xml.Linq.resources.dll
System.Xml.Linq.dll
zh-Hans
System.Xml.Linq.resources.dll
zh-Hant
System.Xml.Linq.resources.dll
BindingXML.csproj.user
obj
Debug
BindingXML.dll
BindingXML.g.resources
BindingXML.pdb
ResolveAssemblyReference.cache
TempPE
pencil.jpg
Properties
BindingXML.Web
App_Data
bin
BindingXML.Web.dll
de
System.Web.Silverlight.resources.dll
es
System.Web.Silverlight.resources.dll
fr
System.Web.Silverlight.resources.dll
it
System.Web.Silverlight.resources.dll
ja
System.Web.Silverlight.resources.dll
ko
System.Web.Silverlight.resources.dll
System.Web.Silverlight.dll
zh-Hans
System.Web.Silverlight.resources.dll
zh-Hant
System.Web.Silverlight.resources.dll
BindingXML.Web.csproj.user
ClientBin
BindingXML.xap
Properties
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)

Share

About the Author

Debdatta Dey
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 9 Mar 2009
Article Copyright 2009 by Debdatta Dey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid