Silverlight XML

Get Microsoft Silverlight

This Silverlight object displays four types of Narcissus flowers. Click a radio button to display an image, the name of the person who developed the flower and year of development.



XAML

<UserControl x:Class="SilverlightDaffodil.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="LayoutRoot" Background="Yellow">
     
        <RadioButton
            x:Name="radioButton1"
            Grid.Column="1"
            Grid.Row="0"
            HorizontalAlignment="Center"
            Margin="0,10,0,0"
            Checked="RadioButton_Checked_1">            
        </RadioButton>
        <RadioButton
            x:Name="radioButton2"
            Grid.Column="1"
            Grid.Row="1"
            HorizontalAlignment="Center"
            Margin="0,10,0,0"
            Checked="RadioButton_Checked_2">            
        </RadioButton>       
        <RadioButton
            x:Name="radioButton3"
            Grid.Column="1"
            Grid.Row="2"
            HorizontalAlignment="Center"
            Margin="0,10,0,0"
            Checked="RadioButton_Checked_3">            
        </RadioButton>  
        <RadioButton
            x:Name="radioButton4"
            Grid.Column="1"
            Grid.Row="3"
            HorizontalAlignment="Center"
            Margin="0,10,0,0"
            Checked="RadioButton_Checked_4">            
        </RadioButton>     
        
        <TextBlock 
            x:Name="text0Block"
            Text="" 
            Grid.Column="0" 
            Grid.Row="0"
            HorizontalAlignment="Right"
            FontFamily="Times Roman"         
            FontWeight="bold"
            FontSize="30"
            Margin="0,0,0,5">            
        </TextBlock>
        <TextBlock
            x:Name="text1Block"
            Text="" 
            Grid.Column="0" 
            Grid.Row="1"
            HorizontalAlignment="Right"           
            FontFamily="Times Roman"         
            FontWeight="bold"
            FontSize="30">            
        </TextBlock>
        <TextBlock 
            x:Name="text2Block"
            Text="" 
            Grid.Column="0" 
            Grid.Row="2"
            HorizontalAlignment="Right"           
            FontFamily="Times Roman"         
            FontWeight="bold"
            FontSize="30">            
        </TextBlock>
        <TextBlock 
            x:Name="text3Block"
            Text="" 
            Grid.Column="0" 
            Grid.Row="3"
            HorizontalAlignment="Right"           
            FontFamily="Times Roman"         
            FontWeight="bold"
            FontSize="30">            
        </TextBlock>
        
        <Image 
            x:Name="carlton"
            Grid.Column="2" 
            Grid.ColumnSpan="2"          
            Grid.Row="0"
            Grid.RowSpan="3"            
            Source="carlton.jpg"            
            Stretch="Uniform"            
            Opacity="0"
            Margin="0,5,0,0"
            HorizontalAlignment="Center">            
        </Image>
        <Image 
            x:Name="tahiti"
            Grid.Column="2" 
            Grid.ColumnSpan="2"          
            Grid.Row="0"
            Grid.RowSpan="3"            
            Source="tahiti.jpg"            
            Stretch="Uniform"            
            Opacity="0"
            Margin="0,5,0,0"
            HorizontalAlignment="Center">
        </Image>
        <Image 
            x:Name="babyMoon"
            Grid.Column="2" 
            Grid.ColumnSpan="2"          
            Grid.Row="0"
            Grid.RowSpan="3"            
            Source="babyMoon.jpg"            
            Stretch="Uniform"            
            Opacity="0"
            Margin="0,5,0,0"
            HorizontalAlignment="Center">
        </Image>
        <Image 
            x:Name="quail"
            Grid.Column="2" 
            Grid.ColumnSpan="2"          
            Grid.Row="0"
            Grid.RowSpan="3"            
            Source="quail.jpg"            
            Stretch="Uniform"            
            Opacity="0"
            Margin="0,5,0,0"
            HorizontalAlignment="Center">
        </Image>
        <TextBlock 
            x:Name="raiserName"
            Grid.Column="2"
            Grid.Row="3"
            Text=""
            Margin="0,5,0,0"
            HorizontalAlignment="Left">            
        </TextBlock>
        <TextBlock
            x:Name="date"
            Grid.Column="3"
            Grid.Row="3"
            Text=""
            Margin="0,5,0,0"
            HorizontalAlignment="Right">            
        </TextBlock>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="60"></RowDefinition>
            <RowDefinition Height="60"></RowDefinition>
            <RowDefinition Height="60"></RowDefinition>
            <RowDefinition Height="60"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="210"></ColumnDefinition>
            <ColumnDefinition Width="50"></ColumnDefinition>
            <ColumnDefinition Width="110"></ColumnDefinition>
            <ColumnDefinition Width="110"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        
    </Grid>
</UserControl>
				
		

Page.xaml.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.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace SilverlightDaffodil
{
    public partial class Page : UserControl
    {       
               
        public Page()
        {
            InitializeComponent();
            loadXML();
            
        }

        private void loadXML()
        {
            WebClient myDaffy = new WebClient();
            myDaffy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myDaffy_DownloadStringCompleted);
            myDaffy.DownloadStringAsync(new Uri("DaffodilPup1.xml", UriKind.Relative));
        }

        void myDaffy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string result = e.Result;
                displayDaffs(result);
            }
           
        }

        daffodil[] allDaffs = new daffodil[4];

        private void displayDaffs(string result)
        {
            XDocument xmlDaffs = XDocument.Parse(result);
            var flowers = from yellowFlower in xmlDaffs.Descendants("Narcissus")
                          select new daffodil
                          {
                              Name = (string)yellowFlower.Attribute("Name"),
                              Price = (string)yellowFlower.Element("Price"),
                              Description = (string)yellowFlower.Element("Description"),
                              Group = (string)yellowFlower.Element("Group"),
                              Year = (string)yellowFlower.Element("Year"),
                              Raiser = (string)yellowFlower.Element("Raiser"),
                          };
            int i = 0;
            foreach (var n in flowers)
            {
                daffodil d = new daffodil();
                d.Name = n.Name.ToString();
                d.Price = n.Price.ToString();
                d.Description = n.Description.ToString();
                d.Group = n.Group.ToString();
                d.Year = n.Year.ToString();
                d.Raiser = n.Raiser.ToString();
                allDaffs[i] = d;
                i++;
            }                      

            text0Block.Text = allDaffs[0].Name;
            text1Block.Text = allDaffs[1].Name;
            text2Block.Text = allDaffs[2].Name;
            text3Block.Text = allDaffs[3].Name;
           
        }
 

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            carlton.Opacity = 1;
            tahiti.Opacity = 0;
            babyMoon.Opacity = 0;
            quail.Opacity = 0;
            raiserName.Text = allDaffs[0].Raiser;
            date.Text = allDaffs[0].Year;
         
        }

        private void RadioButton_Checked_2(object sender, RoutedEventArgs e)
        {
            carlton.Opacity = 0;
            tahiti.Opacity = 1;
            babyMoon.Opacity = 0;
            quail.Opacity = 0;
            raiserName.Text = allDaffs[1].Raiser;
            date.Text = allDaffs[1].Year;
        }

        private void RadioButton_Checked_3(object sender, RoutedEventArgs e)
        {
            carlton.Opacity = 0;
            tahiti.Opacity = 0;
            babyMoon.Opacity = 1;
            quail.Opacity = 0;
            raiserName.Text = allDaffs[2].Raiser;
            date.Text = allDaffs[2].Year;
        }

        private void RadioButton_Checked_4(object sender, RoutedEventArgs e)
        {
            carlton.Opacity = 0;
            tahiti.Opacity = 0;
            babyMoon.Opacity = 0;
            quail.Opacity = 1;
            raiserName.Text = allDaffs[3].Raiser;
            date.Text = allDaffs[3].Year;
        }


    }
}