<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eclipsed4utoo&#039;s Blog&#187; C#</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsed4utoo.com/blog</link>
	<description>Not Your Ordinary Programmer</description>
	<lastBuildDate>Thu, 08 Sep 2011 17:09:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>WP7 &#8211; Using IsolatedStorageSettings</title>
		<link>http://eclipsed4utoo.com/blog/wp7-isolatedstoragesettings/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-isolatedstoragesettings/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 23:00:07 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>
		<category><![CDATA[wpdev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=698</guid>
		<description><![CDATA[In a previous tutorial, I wrote about using the Isolated Storage to store files on a WP7 device. Let&#8217;s say that instead of storing data, you wanted to store settings for your application. This where the IsolatedStorageSettings comes in. The IsolatedStorageSettings is a place to store Key/Value pairs of data. So let&#8217;s say that my [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/" target="_blank">previous tutorial</a>, I wrote about using the Isolated Storage to store files on a WP7 device. Let&#8217;s say that instead of storing data, you wanted to store settings for your application. This where the IsolatedStorageSettings comes in.</p>
<p>The <strong>IsolatedStorageSettings </strong>is a place to store Key/Value pairs of data.</p>
<p>So let&#8217;s say that my application used the Microsoft Ad control, but I wanted to be nice and give the user the ability to turn the ads on and off.</p>
<p>On my Settings screen, I would have a checkbox(<strong>AdsCheckbox</strong>) for allowing the ads. In code, I would save the value by doing this&#8230;</p>
<pre class="brush: csharp;">var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains(&quot;AllowAds&quot;))
    settings[&quot;AllowAds&quot;] = AdsCheckbox.IsChecked.Value;
else
    settings.Add(&quot;AllowAds&quot;, AdsCheckbox.IsChecked.Value);

settings.Save();</pre>
<p>If you will notice, the <strong>ApplicationSettings </strong>object does have a method that allows you to check to see if the key already exists in the Settings. An exception will be thrown if you try to add a key that already exists, so it&#8217;s necessary to do this check before assigning a value.</p>
<p>Also, as you can see, it&#8217;s very easy to get the value out of the settings&#8230;</p>
<pre class="brush: csharp;">// when the form opens, I want to set the Checkbox to being checked
//   or unchecked depending on the setting
var settings = IsolatedStorageSettings.ApplicationSettings;

if (settings.Contains(&quot;AllowAds&quot;))
    AdsCheckbox.IsChecked = bool.Parse(settings[&quot;AllowAds&quot;].ToString());</pre>
<p>However, the IsolatedStorageSettings class isn&#8217;t only for simply datatypes. It can also store your custom class objects.</p>
<p><strong>Employee </strong>class</p>
<pre class="brush: csharp;">public class Employee
{
    public string FullName { get; set; }
    public decimal Salary { get; set; }
}</pre>
<p>Then on my form, I have a button to save an instance of the <strong>Employee </strong>class to the <strong>IsolatedStorageSettings</strong>&#8230;</p>
<pre class="brush: csharp;">private void btnSave_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    Employee emp = new Employee()
    {
        FullName = &quot;John Doe&quot;,
        Salary = 250000
    };

    settings.Add(&quot;Employee1&quot;, emp);
    settings.Save();
}</pre>
<p>I also have a button that will load the object from the <strong>IsolatedStorageSettings</strong>&#8230;</p>
<pre class="brush: csharp;">private void btnLoad_Click(object sender, RoutedEventArgs e)
{
    var settings = IsolatedStorageSettings.ApplicationSettings;

    Employee emp;

    if (settings.TryGetValue&lt;Employee&gt;(&quot;Employee1&quot;, out emp))
    {
         MessageBox.Show(string.Format(&quot;Full Name: {0}\nSalary: {1:c}&quot;, emp.FullName, emp.Salary));
    }
}</pre>
<p>Notice that the <strong>IsolatedStorageSettings.ApplicationSettings</strong> class also has the <strong>TryGetValue </strong>method. This method behaves exactly like the <strong>TryParse </strong>methods of the .Net datatypes. It will return a boolean specifying whether the key was found and whether the conversion from object &#8211;&gt; Employee was successful. If it&#8217;s successful, it will return the instance as the out parameter.</p>
<p>After running the application, clicking the <strong>Save </strong>button, then clicking the <strong>Load </strong>button, we will get this message box&#8230;</p>
<p><img class="alignnone size-full wp-image-699" title="2" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2011/07/2.png" alt="2" width="332" height="170" /></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-isolatedstoragesettings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 &#8211; Working With Isolated Storage</title>
		<link>http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 23:00:28 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>
		<category><![CDATA[wpdev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=692</guid>
		<description><![CDATA[In this tutorial, I will demonstrate how to work with Isolated Storage in Windows Phone 7. This is going to be a very simple tutorial where we create a text file and write data to it. We will then read that data and append more data to it. So first, we need to add these [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, I will demonstrate how to work with Isolated Storage in Windows Phone 7. This is going to be a very simple tutorial where we create a text file and write data to it. We will then read that data and append more data to it.</p>
<p>So first, we need to add these using statements to the top of the code(if they aren&#8217;t already there):</p>
<pre class="brush: csharp;">using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;</pre>
<p>Next we will have our form. Very basic. It has two <strong>Textboxes </strong>and a <strong>Button</strong>. The first <strong>TextBox </strong>will allow the user to type in some text. The <strong>Button </strong>will save the data to <strong>IsolatedStorage</strong>, then read the data from the file and put it into the second <strong>TextBox</strong>.</p>
<p>Here is the XAML:</p>
<pre class="brush: xml;">&lt;Grid x:Name=&quot;ContentGrid&quot; Grid.Row=&quot;1&quot;&gt;

    &lt;TextBox
        Name=&quot;txtText&quot;
        Height=&quot;128&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;8,71,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;460&quot;
        TextWrapping=&quot;Wrap&quot;
        FontSize=&quot;16&quot; /&gt;

    &lt;Button
        Name=&quot;btnSave&quot;
        Content=&quot;Save&quot;
        Click=&quot;btnSave_Click&quot;
        Height=&quot;115&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;62,203,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;336&quot; /&gt;

    &lt;TextBlock
        Name=&quot;textBlock1&quot;
        Text=&quot;Text to add to file:&quot;
        Height=&quot;30&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;24,49,0,0&quot;
        VerticalAlignment=&quot;Top&quot; /&gt;

    &lt;TextBlock
        Name=&quot;textBlock2&quot;
        Text=&quot;Data currently in file:&quot;
        Height=&quot;30&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;24,342,0,0&quot;
        VerticalAlignment=&quot;Top&quot; /&gt;

    &lt;TextBox
        Name=&quot;txtCurrentText&quot;
        Height=&quot;128&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;6,364,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;460&quot;
        TextWrapping=&quot;Wrap&quot;
        FontSize=&quot;16&quot; /&gt;

&lt;/Grid&gt;</pre>
<p>So now we move to the code. We need to create an <strong>IsolatedStorageFile </strong>object:</p>
<pre class="brush: csharp;">using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{

}</pre>
<p>Since we will always be appending to the file, we need to check to make sure the file exists. If it doesn&#8217;t exist, we need to create it.</p>
<pre class="brush: csharp;">// we need to check to see if the file exists
if (!isoStorage.FileExists(fileName))
{
    // file doesn't exist...time to create it.
    isoStorage.CreateFile(fileName);
}</pre>
<p>Once we have done the <strong>FileExists </strong>check, we can now open the file. Once the file has been opened, we can now use the <strong>StreamWriter </strong>class to write to it.</p>
<pre class="brush: csharp;">// since we are appending to the file, we must use FileMode.Append
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
{
    // opens the file and writes to it.
    using (var fileStream = new StreamWriter(isoStream))
    {
        fileStream.WriteLine(txtText.Text);
    }
}</pre>
<p><span style="color: #ff0000;"><strong>Note: notice that we opened the file using FileMode.Append. Because we used this mode, we can&#8217;t read from the file, we can only write to it.</strong></span></p>
<p>So to read from the file, we will need to open the file again, this time using <strong>FileMode.Open</strong>. We are going to do this in another <strong>using </strong>block. Once the file is opened, we use the <strong>StreamReader </strong>class to read it.</p>
<pre class="brush: csharp;">// you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need
//   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
//   we are simply reading the file, we use FileMode.Open
using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
{
    // opens the file and reads it.
    using (var fileStream = new StreamReader(isoStream))
    {
        txtCurrentText.Text = fileStream.ReadToEnd();
    }
}</pre>
<p>And that&#8217;s it. If you run the application, type in the first textbox, hit Save, the data will show up in the bottom textbox. Do it again, and you will see that it has appended to the file.</p>
<p>As you can see, writing to Isolated Storage is really easy.</p>
<p>Here is the entire code for the form&#8230;</p>
<pre class="brush: csharp;">using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;

namespace WP7IsoStorageDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string fileName = &quot;MyTextfile.txt&quot;;

            using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // we need to check to see if the file exists
                if (!isoStorage.FileExists(fileName))
                {
                    // file doesn't exist...time to create it.
                    isoStorage.CreateFile(fileName);
                }

                // since we are appending to the file, we must use FileMode.Append
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
                {
                    // opens the file and writes to it.
                    using (var fileStream = new StreamWriter(isoStream))
                    {
                        fileStream.WriteLine(txtText.Text);
                    }
                }

                // you cannot read from a stream that you opened in FileMode.Append.  Therefore, we need
                //   to close the IsolatedStorageFileStream then open it again in a different FileMode.  Since we
                //   we are simply reading the file, we use FileMode.Open
                using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
                {
                    // opens the file and reads it.
                    using (var fileStream = new StreamReader(isoStream))
                    {
                        txtCurrentText.Text = fileStream.ReadToEnd();
                    }
                }
            }
        }
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-working-isolated-storage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Get Windows Mobile Theme Color</title>
		<link>http://eclipsed4utoo.com/blog/windows-mobile-theme-color/</link>
		<comments>http://eclipsed4utoo.com/blog/windows-mobile-theme-color/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 23:00:01 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[windows mobile]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=676</guid>
		<description><![CDATA[I recently had a business requirement to color the background of a TextBox in Windows Mobile to a specific color.  After completing the tasks, I noticed that the color that was chosen(blue) didn&#8217;t look right on devices that used a different theme like red or green.  So I decided to figure out how to set [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a business requirement to color the background of a TextBox in Windows Mobile to a specific color.  After completing the tasks, I noticed that the color that was chosen(blue) didn&#8217;t look right on devices that used a different theme like red or green.  So I decided to figure out how to set the background color to match the theme color.</p>
<p>The theme system colors are located in the registry entry <strong>HKLM\System\GWE\SysColor</strong>.  This registry entry is a BLOB that contains 29 <strong>DWORD</strong> values that correspond to the different colors of the theme.</p>
<p>For my application, I decided to use the color of the title bar of active windows.  This color is normally the dominate color of the theme.  This color is the third DWORD value in the registry entry.</p>
<p>Each DWORD value in the registry entry is a 4 byte HEX value that corresponds to a specific color.  The first byte is the RED value, second value is the GREEN value, third value is the BLUE value, and the fourth value is always 0.</p>
<p>For this code, I used the <a href="http://www.opennetcf.com/Products/SmartDeviceFramework/tabid/65/Default.aspx" target="_blank">OpenNetCF</a> framework to get the registry values.</p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// Gets the theme color
/// &lt;/summary&gt;
/// &lt;returns&gt; Returns a Color or null&lt;/returns&gt;
public Color? GetThemeColor()
{
    try
    {
         // gets the registry key
         RegistryKey keyColor = Registry.LocalMachine.OpenSubKey(@&quot;System\GWE&quot;);

         // keyColor will be null if registry key was not found
         if (keyColor == null)
             return null;

        byte[] data = (byte[])keyColor.GetValue(&quot;SysColor&quot;);

        if (data[8] != 0 &amp;&amp; data[9] != 0 &amp;&amp; data[10] != 0)
        {
             // the third set of values is for the color of the
             //    title bar of an active window.
             int red = Convert.ToInt32(data[8]);
             int green = Convert.ToInt32(data[9]);
             int blue = Convert.ToInt32(data[10]);

             return Color.FromArgb(red, green, blue);
        }

        return null;
    }
    catch
    {
         return null;
    }
}
</pre>
<p>Usage is pretty simple for the method&#8230;</p>
<pre class="brush: csharp;">
Color? themeColor = GetThemeColor();

if(themeColor != null)
   txtComments.BackColor = themeColor;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/windows-mobile-theme-color/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# &#8211; XML Element Value Generic Extention Method</title>
		<link>http://eclipsed4utoo.com/blog/c-xml-element-value-generic-extention-method/</link>
		<comments>http://eclipsed4utoo.com/blog/c-xml-element-value-generic-extention-method/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 19:00:52 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[extension methods]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=704</guid>
		<description><![CDATA[Recently, I was working on integrating a help desk API into one of our applications.  The API would return XML and I was using LINQ-To-XML to get the values of the elements.  However, a number of the elements in the API were allowed to be null, like a field for the completed date of a [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I was working on integrating a help desk API into one of our applications.  The API would return XML and I was using LINQ-To-XML to get the values of the elements.  However, a number of the elements in the API were allowed to be null, like a field for the completed date of a help desk ticket.  So, I started out writing my code like this&#8230;</p>
<pre class="brush: csharp;">
// constructor for Ticket class
public Ticket(XElement element)
{
     AssignedDate = (!string.IsNullOrEmpty(element.Element(&quot;assigned-date&quot;).Value)) ? DateTime.Parse(element.Element(&quot;assigned-date&quot;).Value) : (DateTime?)null;

     // .... on and one for about 40 elements
}
</pre>
<p>I looked back and thought I should come up with a better way of getting the data from the Element and into the datatype that I needed.  So I came up with this generic extension method&#8230;</p>
<pre class="brush: csharp;">/// &lt;summary&gt;
/// Gets the value of the element.  Will convert the value to specified type if
///    possible.  If not possible, will return default value for type.
/// &lt;/summary&gt;
/// &lt;typeparam name=&quot;T&quot;&gt;Type that should be returned&lt;/typeparam&gt;
/// &lt;param name=&quot;ele&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public static T GetElementValue&lt;T&gt;(this XElement ele)
{
    // if the element is null, then return the default
    if (ele == null)
        return default(T);

    // if value is blank or null, just return the default value
    //   for the type.
    if (string.IsNullOrEmpty(ele.Value))
        return default(T);

    // since the type of the Value property is a string, no need
    //   to do anything fancy.  Just return the value.
    if (typeof(T) == typeof(string))
        return (T)Convert.ChangeType(ele.Value, typeof(T));

    // creates new object of type T to store converted value
    T newObject = default(T);

    // check to see if it's a nullable type.  If so, get the underlying
    //   type.
    // Nullable types don't have a Parse method.  Therefore, we need the
    //   underlying type so we can call the Parse method through reflection
    //   to convert the value.
    PropertyInfo[] properties = typeof(T).GetProperties();
    Type underlyingType = typeof(T);

    // Nullable types will have two properties.  The first is the HasValue
    //   property.  The second is the underlying type.  However, some other
    //   types(such as String), and custom classes/structs could also have
    //   only two properties.  So we check to make sure that there are only
    //   2 properties, then we make sure the first property is the HasValue
    //   property.  Theoretically, this could still give a false positive.
    if (properties.Count() == 2 &amp;&amp;
        string.Equals(properties[0].Name, &quot;HasValue&quot;, StringComparison.InvariantCultureIgnoreCase))
        underlyingType = properties[1].PropertyType;

    try
    {
        // calls Parse method for the type
        MethodInfo method = underlyingType.GetMethod(&quot;Parse&quot;, new[] { typeof(string) });
        newObject = (T)method.Invoke(null, new[] { ele.Value });
    }
    catch (Exception)
    {
        throw;
    }

    return newObject;
}</pre>
<p>Now with this extension method, it really cuts down on the amount of code I have to write.  I can now use it like this..</p>
<pre class="brush: csharp;">
public Ticket(XElement element)
{
     AssignedDate = element.Element(&quot;assigned-date&quot;).GetElementValue&lt;DateTime?&gt;();
}
</pre>
<p>I hope this can help out somebody.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/c-xml-element-value-generic-extention-method/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Generic Serialization Methods</title>
		<link>http://eclipsed4utoo.com/blog/generic-serialization-methods/</link>
		<comments>http://eclipsed4utoo.com/blog/generic-serialization-methods/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 14:19:51 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[serialization]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=664</guid>
		<description><![CDATA[Short blog post today.  These are a couple of generic serialize and deserialize methods that can be easily used when needing to serialize and deserialize classes.  The methods work with any .Net type.  That includes built-in .Net types and custom classes that you might create yourself. These methods will only serialize PUBLIC properties of a [...]]]></description>
			<content:encoded><![CDATA[<p>Short blog post today.  These are a couple of generic serialize and deserialize methods that can be easily used when needing to serialize and deserialize classes.  The methods work with any .Net type.  That includes built-in .Net types and custom classes that you might create yourself.</p>
<p>These methods will only serialize PUBLIC properties of a class.  Also, the XML will be human-readable instead of one long line of text.</p>
<p><strong>Serialize Method</strong></p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
 /// Serializes the data in the object to the designated file path
 /// &lt;/summary&gt;
 /// &lt;typeparam name=&quot;T&quot;&gt;Type of Object to serialize&lt;/typeparam&gt;
 /// &lt;param name=&quot;dataToSerialize&quot;&gt;Object to serialize&lt;/param&gt;
 /// &lt;param name=&quot;filePath&quot;&gt;FilePath for the XML file&lt;/param&gt;
 public static void Serialize&lt;T&gt;(T dataToSerialize, string filePath)
 {
      try
      {
           using (Stream stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite))
           {
                 XmlSerializer serializer = new XmlSerializer(typeof(T));
                 XmlTextWriter writer = new XmlTextWriter(stream, Encoding.Default);
                 writer.Formatting = Formatting.Indented;
                 serializer.Serialize(writer, dataToSerialize);
                 writer.Close();
           }
      }
      catch
      {
           throw;
      }
 }
</pre>
<p><strong>Deserialize Method</strong></p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
 /// Deserializes the data in the XML file into an object
 /// &lt;/summary&gt;
 /// &lt;typeparam name=&quot;T&quot;&gt;Type of object to deserialize&lt;/typeparam&gt;
 /// &lt;param name=&quot;filePath&quot;&gt;FilePath to XML file&lt;/param&gt;
 /// &lt;returns&gt;Object containing deserialized data&lt;/returns&gt;
 public static T Deserialize&lt;T&gt;(string filePath)
 {
      try
      {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T serializedData;

            using (Stream stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                 serializedData = (T)serializer.Deserialize(stream);
            }

            return serializedData;
      }
      catch
      {
            throw;
      }
 }
</pre>
<p>Here is some sample code to show the methods in action.</p>
<pre class="brush: csharp;">
Person p = new Person() { Name = &quot;John Doe&quot;, Age = 42 };
XmlHelper.Serialize&lt;Person&gt;(p, @&quot;D:\text.xml&quot;);

Person p2 = new Person();
p2 = XmlHelper.Deserialize&lt;Person&gt;(@&quot;D:\text.xml&quot;);

Console.WriteLine(&quot;Name: {0}&quot;, p2.Name);
Console.WriteLine(&quot;Age: {0}&quot;, p2.Age);

Console.Read();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/generic-serialization-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 &#8211; Copy/Paste From Clipboard</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-4-copypaste-clipboard/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-4-copypaste-clipboard/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 23:00:20 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[clipboard]]></category>
		<category><![CDATA[Silverlight 4]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=514</guid>
		<description><![CDATA[In this tutorial, I will show how you can use Silverlight 4 to get access to the client clipboard. Silverlight 3 had limited clipboard access, but Microsoft implemented more complete access to the clipboard. For security purposes, access to the clipboard is only allowed through a user-initialed event. For example, you couldn&#8217;t have a timer [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, I will show how you can use Silverlight 4 to get access to the client clipboard. Silverlight 3 had limited clipboard access, but Microsoft implemented more complete access to the clipboard.</p>
<p>For security purposes, access to the clipboard is only allowed through a user-initialed event. For example, you couldn&#8217;t have a timer running in the background to constantly get text from the clipboard.</p>
<p>For first, let&#8217;s create a Silverlight 4 application and name it <strong>SL4ClipboardAccess</strong>. Then Click OK on the next popup.</p>
<p><img class="alignnone size-full wp-image-515" title="10-28-2010 10-05-33 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-28-2010-10-05-33-AM.png" alt="10-28-2010 10-05-33 AM" width="305" height="242" /></p>
<p>I added some controls to the page..</p>
<pre class="brush: csharp;">
&lt;Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;White&quot;&gt;

    &lt;TextBox
        Name=&quot;CopyTextTextBox&quot;
        Height=&quot;23&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;132,36,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;174&quot; /&gt;

    &lt;TextBlock
        Name=&quot;textBlock1&quot;
        Text=&quot;Text To Copy:&quot;
        Height=&quot;23&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;47,40,0,0&quot;
        VerticalAlignment=&quot;Top&quot; /&gt;

    &lt;Button
        Name=&quot;CopyToClipboardButton&quot;
        Content=&quot;Copy To Clipboard&quot;
        Click=&quot;CopyToClipboardButton_Click&quot;
        Height=&quot;39&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;144,81,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;145&quot;  /&gt;

    &lt;Button
        Click=&quot;PasteToTextBoxButton_Click&quot;
        Name=&quot;PasteToTextBoxButton&quot;
        Content=&quot;Paste To Textbox&quot;
        Height=&quot;39&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;144,147,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;145&quot;  /&gt;

    &lt;TextBox
        Name=&quot;PasteTextTextBox&quot;
        Height=&quot;23&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;132,209,0,0&quot;
        VerticalAlignment=&quot;Top&quot;
        Width=&quot;174&quot; /&gt;

    &lt;TextBlock
        Name=&quot;textBlock2&quot;
        Text=&quot;Text From Clipboard:&quot;
        Height=&quot;23&quot;
        HorizontalAlignment=&quot;Left&quot;
        Margin=&quot;8,209,0,0&quot;
        VerticalAlignment=&quot;Top&quot; /&gt;

&lt;/Grid&gt;</pre>
<p>Which look like this&#8230;</p>
<p><img class="alignnone size-full wp-image-517" title="10-28-2010 10-19-03 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-28-2010-10-19-03-AM.png" alt="10-28-2010 10-19-03 AM" width="262" height="186" /></p>
<p>Now, the Click Event for the CopyToClipboard Button would look like this&#8230;</p>
<pre class="brush: csharp;">private void CopyToClipboardButton_Click(object sender, RoutedEventArgs e)
{
    string textToCopy = CopyTextTextBox.Text;

    try
    {
         Clipboard.SetText(textToCopy);
    }
    catch (SecurityException se)
    {
        MessageBox.Show(se.Message);
    }
}</pre>
<p>and the Click Event for the PasteToTextBox Button would be this..</p>
<pre class="brush: csharp;">private void PasteToTextBoxButton_Click(object sender, RoutedEventArgs e)
{
    string textFromClipboard = string.Empty;

    try
    {
         textFromClipboard = Clipboard.GetText();
    }
    catch (SecurityException se)
    {
         MessageBox.Show(se.Message);
    }

    PasteTextTextBox.Text = textFromClipboard;
}</pre>
<p>Now run the application and type into the Text To Copy textbox. When you click the Copy To Clipboard button, you will be prompted for permission for access to the clipboard.</p>
<p><img class="alignnone size-full wp-image-516" title="10-28-2010 10-15-52 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-28-2010-10-15-52-AM.png" alt="10-28-2010 10-15-52 AM" width="402" height="154" /></p>
<p>After allowing access, click the Paste To TextBox button, and the text will now be in the other textbox.</p>
<p>Very simple tutorial for a very powerful feature.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-4-copypaste-clipboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP7 &#8211; Context Menu From Listbox</title>
		<link>http://eclipsed4utoo.com/blog/wp7-context-menu-listbox/</link>
		<comments>http://eclipsed4utoo.com/blog/wp7-context-menu-listbox/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 11:58:40 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[wp7dev]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=428</guid>
		<description><![CDATA[In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox. Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it. To my surprise, even though the OS seems to [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.  To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).  So I decided to roll my own using the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events, and a [b]DispatcherTimer[/b].  Add these two using statements to the top of the code&#8230;
<pre class="brush: plain;"> using System.Collections.ObjectModel; using System.Windows.Threading; </pre>
<p>So first, we need our initial code to get the [b]ListBox [/b]populated.  I created a [b]Person [/b]class just for simplistic reasons.</p>
<pre class="brush: plain;"> public class Person {     public string FirstName { get; set; }     public string LastName { get; set; } } </pre>
<p>Next, I will set up the [b]ListBox [/b]to simply show the [b]FirstName [/b]of the [b]Person[/b].</p>
<pre class="brush: plain;"> &lt;ListBox      Name=&quot;lbNames&quot;      Height=&quot;240&quot;      HorizontalAlignment=&quot;Left&quot;      Margin=&quot;10,119,0,0&quot;      VerticalAlignment=&quot;Top&quot;      Width=&quot;460&quot;     ItemsSource=&quot;{Binding}&quot;&gt;                      &lt;ListBox.ItemTemplate&gt;         &lt;DataTemplate&gt;             &lt;StackPanel&gt;                 &lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;             &lt;/StackPanel&gt;         &lt;/DataTemplate&gt;     &lt;/ListBox.ItemTemplate&gt;                  &lt;/ListBox&gt; </pre>
<p>Now I will create an generic [b]ObservableCollection [/b]object to store the [b]Person [/b]objects.  This will be a class level variable since it will be accessed from multiple events.</p>
<pre class="brush: plain;"> public partial class MainPage : PhoneApplicationPage {     ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();      // Constructor     public MainPage()     {         InitializeComponent();          this.Loaded += new RoutedEventHandler(MainPage_Loaded);     }      void MainPage_Loaded(object sender, RoutedEventArgs e)     {         personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });         personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });         personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });          lbNames.ItemsSource = personList;     } } </pre>
<p>Now I need to create the event handlers for the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events.  This can simply be done by using the [b]Events [/b]list from the [b]Properties [/b]window in the designer.</p>
<pre class="brush: plain;"> private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {  }  private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {  } </pre>
<p>We now need to create two class level objects: a [b]DispatcherTimer [/b]object, and a [b]Person [/b]object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</p>
<pre class="brush: plain;"> ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();  DispatcherTimer timer;  Person selectedPerson = null;  // Constructor public MainPage() {     InitializeComponent();      this.Loaded += new RoutedEventHandler(MainPage_Loaded);      timer = new DispatcherTimer();     timer.Tick += delegate(object s, EventArgs e)     {      }; } </pre>
<p>Next, we will add a [b]Popup [/b]in XAML.  This can go above or below the [b]ListBox[/b] that is currently holding the names.</p>
<pre class="brush: plain;"> &lt;Popup      x:Name=&quot;DeleteContextMenu&quot;      Height=&quot;200&quot;      Width=&quot;400&quot;&gt;      &lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;     &lt;ListBox          x:Name=&quot;lbDeleteContextMenu&quot;         Background=&quot;White&quot;         SelectionChanged=&quot;DeleteContextMenu_SelectionChanged&quot;&gt;          &lt;ListBoxItem             Content=&quot;Delete Person&quot;              Foreground=&quot;Red&quot;             FontSize=&quot;25&quot;             FontWeight=&quot;Bold&quot;/&gt;      &lt;/ListBox&gt;  &lt;/Popup&gt; </pre>
<p>And the event handler for selecting the Delete item&#8230;</p>
<pre class="brush: plain;"> private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {  } </pre>
<p>Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.  So now we move to our code.  First we are going to handle the [b]MouseLeftButtonDown [/b]event.</p>
<pre class="brush: plain;"> private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {     // if there is no person selected, then there is no person to delete     //   no need to do any code if nothing is selected     if (selectedPerson == null)         return;      // gets the position of the mouse cursor to set the Margin     //    of the Popup to show at the mouse coordinates.  You     //    may need to tweak these values to get it to display in     //    the correct location.     Point position = e.GetPosition((UIElement)this);     DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);      // sets the interval to 1.1 seconds.  This means the user will need      //    to hold down on the screen for 1.1 seconds before we determine     //    to show the ContextMenu.     timer.Interval = TimeSpan.FromMilliseconds(1100);     timer.Start(); } </pre>
<p>Next, we will do our code for the [b]MouseLeftButtonUp[/b] event</p>
<pre class="brush: plain;"> private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {     // stop the timer when the user releases the screen     timer.Stop();      // sets the class level variable to the selected row     selectedPerson = lbNames.SelectedItem as Person; } </pre>
<p>Now for our code in the [b]Tick [/b]event for the [b]Timer[/b].</p>
<pre class="brush: plain;"> // Constructor public MainPage() {     InitializeComponent();      this.Loaded += new RoutedEventHandler(MainPage_Loaded);      timer = new DispatcherTimer();     timer.Tick += delegate(object s, EventArgs e)     {         // stop the timer so that it doesn't popup the Context menu again         timer.Stop();          // since we are using the same ListBox over and over, this will         //   make it so when the Context Menu is shown, there will be no         //   selected item from any previous showing of the Context Menu         lbDeleteContextMenu.SelectedIndex = -1;          // opens the Context Menu         DeleteContextMenu.IsOpen = true;     }; } </pre>
<p>Last, we have our code from the [b]SelectionChanged [/b]event for the [b]ListBox [/b]that is part of the Context Menu.</p>
<pre class="brush: plain;"> private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) {     // in the Timer's Tick event, we set the SelectedIndex of the     //   Context Menu's Listbox back to -1.  However, this does     //   fire the SelectionChanged event.  This code will handle that.     if (lbDeleteContextMenu.SelectedIndex == -1)         return;      // closes the Context Menu     DeleteContextMenu.IsOpen = false;      if (selectedPerson == null)         return;      // removes the selected person from the list     personList.Remove(selectedPerson);      // since we are using an ObservableCollection, we do not have     //   to rebind the list to the ListBox.      selectedPerson = null; } </pre>
<p>Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</p>
<div>In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.  To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).</div>
<div>So I decided to roll my own using the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events, and a [b]DispatcherTimer[/b].</div>
<div>Add these two using statements to the top of the code&#8230;</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;using System.Collections.ObjectModel;&lt;/div&gt;
&lt;div&gt;using System.Windows.Threading;&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>So first, we need our initial code to get the [b]ListBox [/b]populated.  I created a [b]Person [/b]class just for simplistic reasons.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;public class Person&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;public string FirstName { get; set; }&lt;/div&gt;
&lt;div&gt;public string LastName { get; set; }&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Next, I will set up the [b]ListBox [/b]to simply show the [b]FirstName [/b]of the [b]Person[/b].</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;ListBox&lt;/div&gt;
&lt;div&gt;Name=&quot;lbNames&quot;&lt;/div&gt;
&lt;div&gt;Height=&quot;240&quot;&lt;/div&gt;
&lt;div&gt;HorizontalAlignment=&quot;Left&quot;&lt;/div&gt;
&lt;div&gt;Margin=&quot;10,119,0,0&quot;&lt;/div&gt;
&lt;div&gt;VerticalAlignment=&quot;Top&quot;&lt;/div&gt;
&lt;div&gt;Width=&quot;460&quot;&lt;/div&gt;
&lt;div&gt;ItemsSource=&quot;{Binding}&quot;&gt;&lt;/div&gt;
&lt;div&gt;&lt;ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;DataTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;StackPanel&gt;&lt;/div&gt;
&lt;div&gt;&lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;&lt;/div&gt;
&lt;div&gt;&lt;/StackPanel&gt;&lt;/div&gt;
&lt;div&gt;&lt;/DataTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;/ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now I will create an generic [b]ObservableCollection [/b]object to store the [b]Person [/b]objects.  This will be a class level variable since it will be accessed from multiple events.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;public partial class MainPage : PhoneApplicationPage&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div&gt;// Constructor&lt;/div&gt;
&lt;div&gt;public MainPage()&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;InitializeComponent();&lt;/div&gt;
&lt;div&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;void MainPage_Loaded(object sender, RoutedEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div&gt;personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });&lt;/div&gt;
&lt;div&gt;lbNames.ItemsSource = personList;&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now I need to create the event handlers for the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events.  This can simply be done by using the [b]Events [/b]list from the [b]Properties [/b]window in the designer.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>We now need to create two class level objects: a [b]DispatcherTimer [/b]object, and a [b]Person [/b]object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div&gt;DispatcherTimer timer;&lt;/div&gt;
&lt;div&gt;Person selectedPerson = null;&lt;/div&gt;
&lt;div&gt;// Constructor&lt;/div&gt;
&lt;div&gt;public MainPage()&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;InitializeComponent();&lt;/div&gt;
&lt;div&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;};&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Next, we will add a [b]Popup [/b]in XAML.  This can go above or below the [b]ListBox[/b] that is currently holding the names.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;Popup&lt;/div&gt;
&lt;div&gt;x:Name=&quot;DeleteContextMenu&quot;&lt;/div&gt;
&lt;div&gt;Height=&quot;200&quot;&lt;/div&gt;
&lt;div&gt;Width=&quot;400&quot;&gt;&lt;/div&gt;
&lt;div&gt;&lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;&lt;/div&gt;
&lt;div&gt;&lt;ListBox&lt;/div&gt;
&lt;div&gt;x:Name=&quot;lbDeleteContextMenu&quot;&lt;/div&gt;
&lt;div&gt;Background=&quot;White&quot;&lt;/div&gt;
&lt;div&gt;SelectionChanged=&quot;DeleteContextMenu_SelectionChanged&quot;&gt;&lt;/div&gt;
&lt;div&gt;&lt;ListBoxItem&lt;/div&gt;
&lt;div&gt;Content=&quot;Delete Person&quot;&lt;/div&gt;
&lt;div&gt;Foreground=&quot;Red&quot;&lt;/div&gt;
&lt;div&gt;FontSize=&quot;25&quot;&lt;/div&gt;
&lt;div&gt;FontWeight=&quot;Bold&quot;/&gt;&lt;/div&gt;
&lt;div&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div&gt;&lt;/Popup&gt;&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>And the event handler for selecting the Delete item&#8230;</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.</div>
<div>So now we move to our code.  First we are going to handle the [b]MouseLeftButtonDown [/b]event.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// if there is no person selected, then there is no person to delete&lt;/div&gt;
&lt;div&gt;//   no need to do any code if nothing is selected&lt;/div&gt;
&lt;div&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div&gt;return;&lt;/div&gt;
&lt;div&gt;// gets the position of the mouse cursor to set the Margin&lt;/div&gt;
&lt;div&gt;//    of the Popup to show at the mouse coordinates.  You&lt;/div&gt;
&lt;div&gt;//    may need to tweak these values to get it to display in&lt;/div&gt;
&lt;div&gt;//    the correct location.&lt;/div&gt;
&lt;div&gt;Point position = e.GetPosition((UIElement)this);&lt;/div&gt;
&lt;div&gt;DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);&lt;/div&gt;
&lt;div&gt;// sets the interval to 1.1 seconds.  This means the user will need&lt;/div&gt;
&lt;div&gt;//    to hold down on the screen for 1.1 seconds before we determine&lt;/div&gt;
&lt;div&gt;//    to show the ContextMenu.&lt;/div&gt;
&lt;div&gt;timer.Interval = TimeSpan.FromMilliseconds(1100);&lt;/div&gt;
&lt;div&gt;timer.Start();&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Next, we will do our code for the [b]MouseLeftButtonUp[/b] event</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// stop the timer when the user releases the screen&lt;/div&gt;
&lt;div&gt;timer.Stop();&lt;/div&gt;
&lt;div&gt;// sets the class level variable to the selected row&lt;/div&gt;
&lt;div&gt;selectedPerson = lbNames.SelectedItem as Person;&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now for our code in the [b]Tick [/b]event for the [b]Timer[/b].</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;// Constructor&lt;/div&gt;
&lt;div&gt;public MainPage()&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;InitializeComponent();&lt;/div&gt;
&lt;div&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// stop the timer so that it doesn't popup the Context menu again&lt;/div&gt;
&lt;div&gt;timer.Stop();&lt;/div&gt;
&lt;div&gt;// since we are using the same ListBox over and over, this will&lt;/div&gt;
&lt;div&gt;//   make it so when the Context Menu is shown, there will be no&lt;/div&gt;
&lt;div&gt;//   selected item from any previous showing of the Context Menu&lt;/div&gt;
&lt;div&gt;lbDeleteContextMenu.SelectedIndex = -1;&lt;/div&gt;
&lt;div&gt;// opens the Context Menu&lt;/div&gt;
&lt;div&gt;DeleteContextMenu.IsOpen = true;&lt;/div&gt;
&lt;div&gt;};&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Last, we have our code from the [b]SelectionChanged [/b]event for the [b]ListBox [/b]that is part of the Context Menu.</div>
<div>
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;// in the Timer's Tick event, we set the SelectedIndex of the&lt;/div&gt;
&lt;div&gt;//   Context Menu's Listbox back to -1.  However, this does&lt;/div&gt;
&lt;div&gt;//   fire the SelectionChanged event.  This code will handle that.&lt;/div&gt;
&lt;div&gt;if (lbDeleteContextMenu.SelectedIndex == -1)&lt;/div&gt;
&lt;div&gt;return;&lt;/div&gt;
&lt;div&gt;// closes the Context Menu&lt;/div&gt;
&lt;div&gt;DeleteContextMenu.IsOpen = false;&lt;/div&gt;
&lt;div&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div&gt;return;&lt;/div&gt;
&lt;div&gt;// removes the selected person from the list&lt;/div&gt;
&lt;div&gt;personList.Remove(selectedPerson);&lt;/div&gt;
&lt;div&gt;// since we are using an ObservableCollection, we do not have&lt;/div&gt;
&lt;div&gt;//   to rebind the list to the ListBox.&lt;/div&gt;
&lt;div&gt;selectedPerson = null;&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;div&gt;</pre>
</div>
<div>Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</div>
<p>In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.  To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).</p>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So I decided to roll my own using the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events, and a [b]DispatcherTimer[/b].</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Add these two using statements to the top of the code&#8230;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;using System.Collections.ObjectModel;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;using System.Windows.Threading;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So first, we need our initial code to get the [b]ListBox [/b]populated.  I created a [b]Person [/b]class just for simplistic reasons.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public class Person&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public string FirstName { get; set; }&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public string LastName { get; set; }&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Next, I will set up the [b]ListBox [/b]to simply show the [b]FirstName [/b]of the [b]Person[/b].</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBox&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Name=&quot;lbNames&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Height=&quot;240&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;HorizontalAlignment=&quot;Left&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Margin=&quot;10,119,0,0&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;VerticalAlignment=&quot;Top&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Width=&quot;460&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ItemsSource=&quot;{Binding}&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;DataTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;StackPanel&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/StackPanel&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/DataTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/ListBox.ItemTemplate&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now I will create an generic [b]ObservableCollection [/b]object to store the [b]Person [/b]objects.  This will be a class level variable since it will be accessed from multiple events.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public partial class MainPage : PhoneApplicationPage&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// Constructor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public MainPage()&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;InitializeComponent();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;void MainPage_Loaded(object sender, RoutedEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;lbNames.ItemsSource = personList;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now I need to create the event handlers for the [b]MouseLeftButtonDown [/b]and [b]MouseLeftButtonUp [/b]events.  This can simply be done by using the [b]Events [/b]list from the [b]Properties [/b]window in the designer.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">We now need to create two class level objects: a [b]DispatcherTimer [/b]object, and a [b]Person [/b]object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DispatcherTimer timer;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Person selectedPerson = null;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// Constructor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public MainPage()&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;InitializeComponent();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;};&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Next, we will add a [b]Popup [/b]in XAML.  This can go above or below the [b]ListBox[/b] that is currently holding the names.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;Popup&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;x:Name=&quot;DeleteContextMenu&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Height=&quot;200&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Width=&quot;400&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBox&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;x:Name=&quot;lbDeleteContextMenu&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Background=&quot;White&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;SelectionChanged=&quot;DeleteContextMenu_SelectionChanged&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;ListBoxItem&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Content=&quot;Delete Person&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Foreground=&quot;Red&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;FontSize=&quot;25&quot;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;FontWeight=&quot;Bold&quot;/&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/ListBox&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;&lt;/Popup&gt;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">And the event handler for selecting the Delete item&#8230;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">So now we move to our code.  First we are going to handle the [b]MouseLeftButtonDown [/b]event.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// if there is no person selected, then there is no person to delete&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   no need to do any code if nothing is selected&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;return;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// gets the position of the mouse cursor to set the Margin&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    of the Popup to show at the mouse coordinates.  You&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    may need to tweak these values to get it to display in&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    the correct location.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;Point position = e.GetPosition((UIElement)this);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// sets the interval to 1.1 seconds.  This means the user will need&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    to hold down on the screen for 1.1 seconds before we determine&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//    to show the ContextMenu.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Interval = TimeSpan.FromMilliseconds(1100);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Start();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Next, we will do our code for the [b]MouseLeftButtonUp[/b] event</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// stop the timer when the user releases the screen&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Stop();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// sets the class level variable to the selected row&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;selectedPerson = lbNames.SelectedItem as Person;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now for our code in the [b]Tick [/b]event for the [b]Timer[/b].</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// Constructor&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;public MainPage()&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;InitializeComponent();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;this.Loaded += new RoutedEventHandler(MainPage_Loaded);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer = new DispatcherTimer();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Tick += delegate(object s, EventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// stop the timer so that it doesn't popup the Context menu again&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;timer.Stop();&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// since we are using the same ListBox over and over, this will&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   make it so when the Context Menu is shown, there will be no&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   selected item from any previous showing of the Context Menu&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;lbDeleteContextMenu.SelectedIndex = -1;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// opens the Context Menu&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DeleteContextMenu.IsOpen = true;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;};&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Last, we have our code from the [b]SelectionChanged [/b]event for the [b]ListBox [/b]that is part of the Context Menu.</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<pre class="brush: plain;">&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;private void DeleteContextMenu_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;{&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// in the Timer's Tick event, we set the SelectedIndex of the&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   Context Menu's Listbox back to -1.  However, this does&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   fire the SelectionChanged event.  This code will handle that.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;if (lbDeleteContextMenu.SelectedIndex == -1)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;return;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// closes the Context Menu&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;DeleteContextMenu.IsOpen = false;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;if (selectedPerson == null)&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;return;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// removes the selected person from the list&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;personList.Remove(selectedPerson);&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;// since we are using an ObservableCollection, we do not have&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;//   to rebind the list to the ListBox.&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;selectedPerson = null;&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;}&lt;/div&gt;
&lt;div id=&quot;_mcePaste&quot; style=&quot;position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot;&gt;</pre>
</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</div>
<p>In writing one of my Windows Phone 7 applications, I needed to do a allow the user to delete an entry from a ListBox.  Since my ListBox didn&#8217;t have enough room for an actual delete button, I decided I would use a ContextMenu to do it.</p>
<p>To my surprise, even though the OS seems to support a long press action(the way to uninstall an application), that action doesn&#8217;t seem to be available to developers(or not that I could find).</p>
<div>So I decided to roll my own using the <strong>MouseLeftButtonDown </strong>and <strong>MouseLeftButtonUp </strong>events, and a <strong>DispatcherTimer</strong>.</div>
<div>Add these two using statements to the top of the code&#8230;</div>
<div>
<pre class="brush: csharp;">
using System.Collections.ObjectModel;
using System.Windows.Threading;
</pre>
</div>
<p>So first, we need our initial code to get the <strong>ListBox </strong>populated.  I created a <strong>Person </strong>class just for simplistic reasons.</p>
<div>
<pre class="brush: csharp;">
public class Person
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}
</pre>
</div>
<p>Next, I will set up the <strong>ListBox </strong>to simply show the <strong>FirstName</strong> of the <strong>Person</strong>.</p>
<div>
<pre class="brush: csharp;">
&lt;ListBox
     Name=&quot;lbNames&quot;
     Height=&quot;240&quot;
     HorizontalAlignment=&quot;Left&quot;
     Margin=&quot;10,119,0,0&quot;
     VerticalAlignment=&quot;Top&quot;
     Width=&quot;460&quot;
     ItemsSource=&quot;{Binding}&quot;&gt;

     &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
               &lt;StackPanel&gt;
                    &lt;TextBlock Text=&quot;{Binding FirstName}&quot; /&gt;
               &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
     &lt;/ListBox.ItemTemplate&gt;
&lt;/ListBox&gt;
</pre>
</div>
<p>Now I will create an generic <strong>ObservableCollection </strong>object to store the <strong>Person </strong>objects.  This will be a class level variable since it will be accessed from multiple events.</p>
<div>
<pre class="brush: csharp;">public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Doe&quot; });
        personList.Add(new Person() { FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot; });
        personList.Add(new Person() { FirstName = &quot;John&quot;, LastName = &quot;Adams&quot; });

        lbNames.ItemsSource = personList;
    }
}</pre>
</div>
<p>Now I need to create the event handlers for the <strong>MouseLeftButtonDown </strong>and <strong>MouseLeftButtonUp</strong> events.  This can simply be done by using the <strong>Events </strong>list from the <strong>Properties </strong>window in the designer.</p>
<div>
<pre class="brush: csharp;">private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{

}

private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{

}</pre>
</div>
<p>We now need to create two class level objects: a <strong>DispatcherTimer </strong>object, and a <strong>Person </strong>object.  These are class level because they will need to be accessed from different events.  We will also subscribe to the Tick event for the timer.</p>
<div>
<pre class="brush: csharp;">ObservableCollection&lt;Person&gt; personList = new ObservableCollection&lt;Person&gt;();

DispatcherTimer timer;

Person selectedPerson = null;

// Constructor
public MainPage()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    timer = new DispatcherTimer();
    timer.Tick += delegate(object s, EventArgs e)
    {

    };
}</pre>
</div>
<p>Next, we will add a <strong>Popup </strong>in XAML.  This can go above or below the <strong>ListBox </strong>that is currently holding the names.</p>
<div>
<pre class="brush: csharp;">&lt;Popup
    x:Name=&quot;DeleteContextMenu&quot;
    Height=&quot;200&quot;
    Width=&quot;400&quot;&gt;

    &lt;!-- This is a ListBox as an ItemTemplate for the Popup --&gt;
    &lt;ListBox
        x:Name=&quot;lbDeleteContextMenu&quot;
        Background=&quot;White&quot;
        Selectionchanged=&quot;DeleteContextMenu_Selectionchanged&quot;&gt;

        &lt;ListBoxItem
            Content=&quot;Delete Person&quot;
            Foreground=&quot;Red&quot;
            FontSize=&quot;25&quot;
            FontWeight=&quot;Bold&quot;/&gt;

    &lt;/ListBox&gt;

&lt;/Popup&gt;</pre>
</div>
<p>And the event handler for selecting the Delete item&#8230;</p>
<div>
<pre class="brush: csharp;">private void DeleteContextMenu_Selectionchanged(object sender, System.Windows.Controls.SelectionchangedEventArgs e)
{

}</pre>
</div>
<p>Here is how it&#8217;s going to work.  When the user presses down, we will start the timer.  When the user releases, then we stop the timer.  So if the timer&#8217;s interval is reached, we know that the user was holding down on the screen, so we will display the popup.</p>
<p>So now we move to our code.  First we are going to handle the <strong>MouseLeftButtonDown </strong>event.</p>
<pre class="brush: csharp;">private void lbNames_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // if there is no person selected, then there is no person to delete
    //   no need to do any code if nothing is selected
    if (selectedPerson == null)
        return;

    // gets the position of the mouse cursor to set the Margin
    //    of the Popup to show at the mouse coordinates.  You
    //    may need to tweak these values to get it to display in
    //    the correct location.
    Point position = e.GetPosition((UIElement)this);
    DeleteContextMenu.Margin = new Thickness(position.X, position.Y - 200, 20, 0);

    // sets the interval to 1.1 seconds.  This means the user will need
    //    to hold down on the screen for 1.1 seconds before we determine
    //    to show the ContextMenu.
    timer.Interval = TimeSpan.FromMilliseconds(1100);
    timer.Start();
}</pre>
<p><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">Next, we will do our code for the <strong>MouseLeftButtonUp</strong> event</span></p>
<div>
<pre class="brush: csharp;">private void lbNames_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // stop the timer when the user releases the screen
    timer.Stop();

    // sets the class level variable to the selected row
    selectedPerson = lbNames.SelectedItem as Person;
}</pre>
</div>
<p>Now for our code in the <strong>Tick </strong>event for the <strong>Timer</strong>.</p>
<div>
<pre class="brush: csharp;">// Constructor
public MainPage()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    timer = new DispatcherTimer();
    timer.Tick += delegate(object s, EventArgs e)
    {
        // stop the timer so that it doesn't popup the Context menu again
        timer.Stop();

        // since we are using the same ListBox over and over, this will
        //   make it so when the Context Menu is shown, there will be no
        //   selected item from any previous showing of the Context Menu
        lbDeleteContextMenu.SelectedIndex = -1;

        // opens the Context Menu
        DeleteContextMenu.IsOpen = true;
    };
}</pre>
</div>
<p>Last, we have our code from the <strong>SelectionChanged </strong>event for the <strong>ListBox </strong>that is part of the Context Menu.</p>
<div>
<pre class="brush: csharp;">private void DeleteContextMenu_Selectionchanged(object sender, System.Windows.Controls.SelectionchangedEventArgs e)
{
    // in the Timer's Tick event, we set the SelectedIndex of the
    //   Context Menu's Listbox back to -1.  However, this does
    //   fire the Selectionchanged event.  This code will handle that.
    if (lbDeleteContextMenu.SelectedIndex == -1)
        return;

    // closes the Context Menu
    DeleteContextMenu.IsOpen = false;

    if (selectedPerson == null)
        return;

    // removes the selected person from the list
    personList.Remove(selectedPerson);

    // since we are using an ObservableCollection, we do not have
    //   to rebind the list to the ListBox.

    selectedPerson = null;
}</pre>
</div>
<p>Now you can run the application, and you will see the list show up.  You must click on an item first before clicking and holding to show the Context Menu.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wp7-context-menu-listbox/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WPF &#8211; Dynamically Loading LINQ-To-SQL Tables and Columns Into ComboBox</title>
		<link>http://eclipsed4utoo.com/blog/wpf-loading-linqtosql-tables-columns-combobox/</link>
		<comments>http://eclipsed4utoo.com/blog/wpf-loading-linqtosql-tables-columns-combobox/#comments</comments>
		<pubDate>Tue, 25 May 2010 22:00:21 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ-To-SQL]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=338</guid>
		<description><![CDATA[Here is some code to dynamically load the table names into a ComboBox.  Then on the selection of a specific table, load the columns for that table into a second ComboBox. First is the XAML: &#60;Window x:Class="WpfApplication4.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525"&#62;    &#60;Grid&#62;      [...]]]></description>
			<content:encoded><![CDATA[<p>Here is some code to dynamically load the table names into a ComboBox.  Then on the selection of a specific table, load the columns for that table into a second ComboBox.</p>
<p>First is the XAML:</p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #000088; padding: 0px; margin: 0px;">&lt;Window</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">x:Class</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"WpfApplication4.MainWindow"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns:x</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #660066; padding: 0px; margin: 0px;">Title</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"MainWindow"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">Height</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"350"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">Width</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"525"</span><span style="color: #000088; padding: 0px; margin: 0px;">&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;Grid&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;ComboBox</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Name</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"cboTables"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Height</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"23"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">HorizontalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Left"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Margin</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"186,60,0,0"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">VerticalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Top"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Width</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"120"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">ItemsSource</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"{Binding}"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Selectionchanged</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"cboTables_Selectionchanged"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">DisplayMemberPath</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"RowType.Name"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">SelectedValuePath</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"RowType.DataMembers"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #000088; padding: 0px; margin: 0px;">/&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        <br style="padding: 0px; margin: 0px;" />        </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;ComboBox</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Height</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"23"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">HorizontalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Left"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Margin</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"186,107,0,0"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Name</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"cboColumns"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">VerticalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Top"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Width</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"120"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">ItemsSource</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"{Binding}"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">DisplayMemberPath</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"MappedName"</span><span style="color: #000088; padding: 0px; margin: 0px;">/&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;Label</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Name</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"label1"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Content</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Tables:"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Height</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"28"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">HorizontalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Left"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Margin</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"134,60,0,0"</span><span style="color: #000000; padding: 0px; margin: 0px;">  <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">VerticalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Top"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #000088; padding: 0px; margin: 0px;">/&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;Label</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Name</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"label2"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Content</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Columns:"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Height</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"28"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">HorizontalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Left"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Margin</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"121,107,0,0"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">VerticalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Top"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #000088; padding: 0px; margin: 0px;">/&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;/Grid&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;/Window&gt;</span></pre>
<p>Notice that we have set the <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">ItemsSource</span> attribute to <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">{Binding}</span>. This lets the control know that we are binding data to the control(big surprise). Notice that I have also set the<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">DisplayMemberPath</span> and <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">SelectedValuePath</span> in the Tables ComboBox and the<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">DisplayMemberPath</span> in the Columns ComboBox.</p>
<p>Next is the code:</p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Collections</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">ObjectModel</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Windows</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Windows</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Controls</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">namespace</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">WpfApplication4</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">/// &lt;summary&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">/// Interaction logic for Mainwindow.xaml</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">/// &lt;/summary&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">partial</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">class</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainWindow</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">:</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">Window</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainWindow</span><span style="color: #999900; padding: 0px; margin: 0px;">()</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #990099; padding: 0px; margin: 0px;">InitializeComponent</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">this</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Loaded</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">+=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">RoutedEventHandler</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">MainWindow_Loaded</span><span style="color: #999900; padding: 0px; margin: 0px;">);</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">void</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainWindow_Loaded</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">object</span><span style="color: #000000; padding: 0px; margin: 0px;"> sender</span><span style="color: #999900; padding: 0px; margin: 0px;">,</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">RoutedEventArgs</span><span style="color: #000000; padding: 0px; margin: 0px;"> e</span><span style="color: #999900; padding: 0px; margin: 0px;">)</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #008200; padding: 0px; margin: 0px;">// creates a new instance of our DataContext</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">InformationDataContext</span><span style="color: #000000; padding: 0px; margin: 0px;"> db </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">InformationDataContext</span><span style="color: #999900; padding: 0px; margin: 0px;">())</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #008200; padding: 0px; margin: 0px;">// gets the list of tables</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">var</span><span style="color: #000000; padding: 0px; margin: 0px;"> tableList </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> db</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Mapping</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">GetTables</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                cboTables</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">ItemsSource</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> tableList</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">private</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">void</span><span style="color: #000000; padding: 0px; margin: 0px;"> cboTables_Selectionchanged</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">object</span><span style="color: #000000; padding: 0px; margin: 0px;"> sender</span><span style="color: #999900; padding: 0px; margin: 0px;">,</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">SelectionchangedEventArgs</span><span style="color: #000000; padding: 0px; margin: 0px;"> e</span><span style="color: #999900; padding: 0px; margin: 0px;">)</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            cboColumns</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">ItemsSource</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> cboTables</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">SelectedValue</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">as</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">ReadOnlyCollection</span><span style="color: #999900; padding: 0px; margin: 0px;">&lt;</span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Data</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Linq</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Mapping</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">MetaDataMember</span><span style="color: #999900; padding: 0px; margin: 0px;">&gt;;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">}</span></pre>
<p>We use the <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">GetTables</span> method of the DataContext class to get the list of tables. The objects themselves are of type <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">System.Data.Linq.Mapping.AttributedMetaTable</span>. Two properties of this class are <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">RowType.Name</span> and <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">RowType.DataMembers</span>. The <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">RowType.Name</span> is the name of the table without the &#8220;dbo&#8221; in front. The <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">RowType.DataMembers</span> is a list of the columns of that table.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />The <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">RowType.DataMembers</span> objects are of type <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">System.Data.Linq.Mapping.MetaDataMember</span>. One property of this class is the <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">MappedName</span> property that we are using as the <span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; color: #000000; font-family: monospace !important; background-position: initial initial; background-repeat: initial initial; padding: 2px; margin: 0px; border: 1px dotted #000000;">DisplayMemberPath</span>. This gives us the name of the column without any extra data.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wpf-loading-linqtosql-tables-columns-combobox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WPF &#8211; Create Animation Programmatically</title>
		<link>http://eclipsed4utoo.com/blog/wpf-create-animation-programmatically/</link>
		<comments>http://eclipsed4utoo.com/blog/wpf-create-animation-programmatically/#comments</comments>
		<pubDate>Thu, 20 May 2010 22:00:48 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[storyboard]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=332</guid>
		<description><![CDATA[This is a short code snippet on creating an animation in WPF through code. In my Twitter app that I have been working on, I was recently doing some optimizations.  When I first did the app, I was new to WPF and knew nothing about animations.  After spending time in Silverlight, I used that knowledge [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short code snippet on creating an animation in WPF through code.</p>
<p>In my <a href="http://eclipsed4utoo.com/blog/twiteclipse-twitter-desktop-client/" target="_blank">Twitter</a> app that I have been working on, I was recently doing some optimizations.  When I first did the app, I was new to WPF and knew nothing about animations.  After spending time in Silverlight, I used that knowledge to do some animations in WPF.</p>
<p>I wanted to do simple &#8220;fade-in&#8221; and &#8220;fade-out&#8221; animations when removing tweets from view and adding new tweets to the view.   And being the person that I am, I like doing this stuff in code.  While I like XAML a lot, I still like writing the code.  So here is how to do an animation that changes the Opacity property programmatically.</p>
<pre class="brush: csharp;">
// because I am doing these as extension methods, they
//   will be available to all UIElement objects, which are
//   basically all controls that can be added to the GUI
public static class ControlAnimationExtensionMethods
{
    public static void FadeIn(this UIElement targetControl)
    {
        DoubleAnimation fadeInAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1.5)));
        Storyboard.SetTarget(fadeInAnimation, targetControl);
        Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(fadeInAnimation);
        sb.Begin();
    }

    public static void FadeOut(this UIElement targetControl)
    {
        DoubleAnimation fadeInAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(1.5)));
        Storyboard.SetTarget(fadeInAnimation, targetControl);
        Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath(UIElement.OpacityProperty));
        Storyboard sb = new Storyboard();
        sb.Children.Add(fadeInAnimation);
        sb.Begin();
    }
}
</pre>
<p>This code is useful when dynamically creating controls that you want to do animations on.</p>
<pre class="brush: csharp;">
TextBlock tb = new TextBlock();
tb.Name = &quot;textBlock1&quot;;
// set more property values

tb.FadeIn();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wpf-create-animation-programmatically/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 &#8211; Connecting To Remote Database With WCF</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-connecting-remote-database-wcf/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-connecting-remote-database-wcf/#comments</comments>
		<pubDate>Fri, 14 May 2010 22:00:36 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=314</guid>
		<description><![CDATA[This tutorial will show how to connect to a database that is located on the deploy server from a Silverlight application. I will be using Visual Studio 2010 and Silverlight 4. So first, we will create a Silverlight 4 application. Next, we are just going to put a DataGrid on our MainPage.xaml. &#60;UserControl x:Class="SilverlightWCFTutorial.MainPage"    [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will show how to connect to a database that is located on the deploy server from a Silverlight application.</p>
<p>I will be using Visual Studio 2010 and Silverlight 4.</p>
<p>So first, we will create a Silverlight 4 application.</p>
<p><img class="alignnone size-full wp-image-316" title="Thumb1" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/05/Thumb1.png" alt="Thumb1" width="200" height="116" /></p>
<p><img class="alignnone size-full wp-image-318" title="Image2" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/05/Image21.PNG" alt="Image2" width="218" height="175" /></p>
<p>Next, we are just going to put a DataGrid on our MainPage.xaml.</p>
<pre><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;">
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #000088; padding: 0px; margin: 0px;">&lt;UserControl</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">x:Class</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"SilverlightWCFTutorial.MainPage"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns:x</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns:d</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.microsoft.com/exp</span><strong> </strong><span style="color: #008800; padding: 0px; margin: 0px;">ression/blend/2008"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns:mc</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.openxmlformats.org/markup-compatibility/2006"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #660066; padding: 0px; margin: 0px;">mc:Ignorable</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"d"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #660066; padding: 0px; margin: 0px;">d:DesignHeight</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"300"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">d:DesignWidth</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"400"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">xmlns:sdk</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"</span><span style="color: #000088; padding: 0px; margin: 0px;">&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;Grid</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">x:Name</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"LayoutRoot"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #660066; padding: 0px; margin: 0px;">Background</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"White"</span><span style="color: #000088; padding: 0px; margin: 0px;">&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;sdk:DataGrid</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Name</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"dataGridPerson"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">AutoGenerateColumns</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"True"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Height</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"206"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">HorizontalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Left"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Margin</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"12,44,0,0"</span><span style="color: #000000; padding: 0px; margin: 0px;">  <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">VerticalAlignment</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"Top"</span><span style="color: #000000; padding: 0px; margin: 0px;"> <br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">Width</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"376"</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #660066; padding: 0px; margin: 0px;">ItemsSource</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #008800; padding: 0px; margin: 0px;">"{Binding}"</span><span style="color: #000088; padding: 0px; margin: 0px;">&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;/sdk:DataGrid&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;/Grid&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #000088; padding: 0px; margin: 0px;">&lt;/UserControl&gt;</span></pre>
<p></span></pre>
<p>So for the Grid, we are simply going to show a list of names that are in a database table.  I have a database table setup as:</p>
<p>Table Name:   Person</p>
<p>Columns:<br />
ID<br />
FirstName<br />
LastName<br />
Age</p>
<p>I am going to concatenate the First Name and Last Name, then bind that to the grid.</p>
<p>Now that we have our Silverlight application ready, we will add a WCF service to our existing &#8220;SilverlightWCFTutorial.Web&#8221; project that was automatically created for us.</p>
<p><img class="alignnone size-full wp-image-320" title="Image3" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/05/Image3.PNG" alt="Image3" width="479" height="269" /></p>
<p>Now we are going to setup the interface that was created for us.  The interface automatically creates a method called &#8220;DoWork&#8221;.  We are going to change the name of this to be a little more specific.  I gave the method the name of &#8220;GetNames&#8221;.</p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;"> </span></p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #999900; padding: 0px; margin: 0px;">[</span><span style="color: #990099; padding: 0px; margin: 0px;">ServiceContract</span><span style="color: #999900; padding: 0px; margin: 0px;">]</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">interface</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">IDatabaseService</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">[</span><span style="color: #990099; padding: 0px; margin: 0px;">OperationContract</span><span style="color: #999900; padding: 0px; margin: 0px;">]</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #990099; padding: 0px; margin: 0px;">List</span><span style="color: #646464; padding: 0px; margin: 0px;">&lt;string&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">GetNames</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">}</span></pre>
<p>Now that the interface is finished, we will move to the service file.  Double-click on the &#8220;DatabaseService.svc&#8221; file to get to the code.  This code is just going to run some simple ADO to get the data from the database.</p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;"> </span></p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Collections</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Generic</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Linq</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Runtime</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Serialization</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">ServiceModel</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Text</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Data</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">SqlClient</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Data</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">namespace</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">SilverlightWcfService</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "DatabaseService" in code, svc and config file together.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">class</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">DatabaseService</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">:</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">IDatabaseService</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">string</span><span style="color: #000000; padding: 0px; margin: 0px;"> myConnectionString </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #646464; padding: 0px; margin: 0px;">"someConnectionString"</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">List</span><span style="color: #646464; padding: 0px; margin: 0px;">&lt;string&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">GetNames</span><span style="color: #999900; padding: 0px; margin: 0px;">()</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #990099; padding: 0px; margin: 0px;">List</span><span style="color: #646464; padding: 0px; margin: 0px;">&lt;string&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"> list </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">List</span><span style="color: #646464; padding: 0px; margin: 0px;">&lt;string&gt;</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">SqlConnection</span><span style="color: #000000; padding: 0px; margin: 0px;"> cn </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">SqlConnection</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #000000; padding: 0px; margin: 0px;">myConnectionString</span><span style="color: #999900; padding: 0px; margin: 0px;">))</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">SqlCommand</span><span style="color: #000000; padding: 0px; margin: 0px;"> cmd </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> cn</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">CreateCommand</span><span style="color: #999900; padding: 0px; margin: 0px;">())</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                    cmd</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">CommandText</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #646464; padding: 0px; margin: 0px;">"SELECT FirstName + ' ' + LastName FROM Person"</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                    cmd</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">CommandType</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">CommandType</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Text</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />                    cn</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Open</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />                    </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">SqlDataReader</span><span style="color: #000000; padding: 0px; margin: 0px;"> dr </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> cmd</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">ExecuteReader</span><span style="color: #999900; padding: 0px; margin: 0px;">())</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                    </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">while</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #000000; padding: 0px; margin: 0px;">dr</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Read</span><span style="color: #999900; padding: 0px; margin: 0px;">())</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                            list</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Add</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #000000; padding: 0px; margin: 0px;">dr</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">GetString</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006666; padding: 0px; margin: 0px;">0</span><span style="color: #999900; padding: 0px; margin: 0px;">));</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                    </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">return</span><span style="color: #000000; padding: 0px; margin: 0px;"> list</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">}</span></pre>
<p>Next, we will need to fix the &#8220;Markup&#8221; of the &#8220;DatabaseService.svc&#8221; file.   Right-click on the file, then choose &#8220;View Markup&#8221;.  In the markup, you will notice a &#8220;Service&#8221; attribute.  This should be in the format of &#8220;Namespace.ServiceName&#8221;.  In our case, the autogenerated value could be incorrect.  Change the code to this..</p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;"> </span></p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #999900; padding: 0px; margin: 0px;">&lt;%@</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">ServiceHost</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">Language</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #646464; padding: 0px; margin: 0px;">"C#"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">Debug</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #646464; padding: 0px; margin: 0px;">"true"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">Service</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #646464; padding: 0px; margin: 0px;">"SilverlightWCFTutorial.Web.DatabaseService"</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">CodeBehind</span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #646464; padding: 0px; margin: 0px;">"DatabaseService.svc.cs"</span><span style="color: #000000; padding: 0px; margin: 0px;"> %&gt;</span></pre>
<p>Now that our WCF service is complete, we are going back to the Silverlight app.  We are going to add an event handler for the &#8220;Loaded&#8221; event of the MainPage.xaml.</p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;"> </span></p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage</span><span style="color: #999900; padding: 0px; margin: 0px;">()</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #990099; padding: 0px; margin: 0px;">InitializeComponent</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">this</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Loaded</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">+=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">RoutedEventHandler</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage_Loaded</span><span style="color: #999900; padding: 0px; margin: 0px;">);</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">}</span></pre>
<p>You will need to rebuild your solution at this point.</p>
<p>We will also need to add a Reference to the Service for our Silverlight project.  Right-click on the &#8220;SilverlightWCFTutorial&#8221; project, and choose &#8220;Add Service Reference&#8230;&#8221;.  Once the Add Service Reference window comes up, click the &#8220;Discover&#8221; button.  This will find our service.  Fill in the &#8220;Namespace&#8221;(called mine &#8220;MyDatabaseService&#8221;).  Then click OK.</p>
<p><img class="alignnone size-full wp-image-322" title="Image4" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/05/Image4.PNG" alt="Image4" width="377" height="304" /></p>
<p>Now we are ready to do some code.  Remember that the requests in Silverlight are made asynchronously.</p>
<p>In our &#8220;MainPage_Loaded&#8217; event handler, we will have this code..</p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;"> </span></p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">void</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage_Loaded</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">object</span><span style="color: #000000; padding: 0px; margin: 0px;"> sender</span><span style="color: #999900; padding: 0px; margin: 0px;">,</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">RoutedEventArgs</span><span style="color: #000000; padding: 0px; margin: 0px;"> e</span><span style="color: #999900; padding: 0px; margin: 0px;">)</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">// The DatabaseServiceClient class was automatically created for us.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">// The autogeneration takes the name of the service, then appends "Client" to it,</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">//   giving us the "DatabaseServiceClient" class.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #990099; padding: 0px; margin: 0px;">DatabaseServiceClient</span><span style="color: #000000; padding: 0px; margin: 0px;"> client </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">DatabaseServiceClient</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">// Since requests in Silverlight are asynchronous, we have a Completed method</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #008200; padding: 0px; margin: 0px;">//   that will be fired when the request has been completed.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    client</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">GetNamesCompleted</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">+=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">delegate</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">object</span><span style="color: #000000; padding: 0px; margin: 0px;"> s</span><span style="color: #999900; padding: 0px; margin: 0px;">,</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">GetNamesCompletedEventArgs</span><span style="color: #000000; padding: 0px; margin: 0px;"> es</span><span style="color: #999900; padding: 0px; margin: 0px;">)</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #008200; padding: 0px; margin: 0px;">// when the request has been completed, we want to bind the data to the grid.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #008200; padding: 0px; margin: 0px;">// the Result property of the EventArgs contains the returned data.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #008200; padding: 0px; margin: 0px;">// ObservableCollection is a common collection that is used when databinding to</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #008200; padding: 0px; margin: 0px;">//    a DataGrid.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #990099; padding: 0px; margin: 0px;">ObservableCollection</span><span style="color: #646464; padding: 0px; margin: 0px;">&lt;string&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"> myList </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> es</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Result</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />        dataGridPerson</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">DataContext</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> myList</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">};</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />    client</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">GetNamesAsync</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">}</span></pre>
<p>And that&#8217;s it.  We now run the Silverlight app, and we get this&#8230;</p>
<p><img class="alignnone size-full wp-image-323" title="Image5" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/05/Image5.PNG" alt="Image5" width="303" height="193" /></p>
<p>Here is the full code of the MainPage.xaml&#8230;</p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; border-collapse: collapse; color: #222222;"> </span></p>
<pre style="font-size: 14px !important; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; font-family: monospace !important; overflow-x: scroll; overflow-y: scroll; max-height: 500px; width: 450px; display: block; background-position: initial initial; background-repeat: initial initial; padding: 12px; margin: 0px; border: 1px dotted #000000;"><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Collections</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">ObjectModel</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Windows</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">System</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Windows</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Controls</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">using</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">SilverlightWCFTutorial</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">MyDatabaseService</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" /></span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">namespace</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">SilverlightWCFTutorial</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">partial</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">class</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">:</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">UserControl</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">public</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage</span><span style="color: #999900; padding: 0px; margin: 0px;">()</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #990099; padding: 0px; margin: 0px;">InitializeComponent</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">this</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Loaded</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">+=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">RoutedEventHandler</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage_Loaded</span><span style="color: #999900; padding: 0px; margin: 0px;">);</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">void</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">MainPage_Loaded</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">object</span><span style="color: #000000; padding: 0px; margin: 0px;"> sender</span><span style="color: #999900; padding: 0px; margin: 0px;">,</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">RoutedEventArgs</span><span style="color: #000000; padding: 0px; margin: 0px;"> e</span><span style="color: #999900; padding: 0px; margin: 0px;">)</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #008200; padding: 0px; margin: 0px;">// The DatabaseServiceClient class was automatically created for us.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #008200; padding: 0px; margin: 0px;">// The autogeneration takes the name of the service, then appends "Client" to it,</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #008200; padding: 0px; margin: 0px;">//   giving us the "DatabaseServiceClient" class.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #990099; padding: 0px; margin: 0px;">DatabaseServiceClient</span><span style="color: #000000; padding: 0px; margin: 0px;"> client </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">new</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">DatabaseServiceClient</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #008200; padding: 0px; margin: 0px;">// Since requests in Silverlight are asynchronous, we have a Completed method</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #008200; padding: 0px; margin: 0px;">//   that will be fired when the request has been completed.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            client</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">GetNamesCompleted</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">+=</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">delegate</span><span style="color: #999900; padding: 0px; margin: 0px;">(</span><span style="color: #006699; font-weight: bold; padding: 0px; margin: 0px;">object</span><span style="color: #000000; padding: 0px; margin: 0px;"> s</span><span style="color: #999900; padding: 0px; margin: 0px;">,</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #990099; padding: 0px; margin: 0px;">GetNamesCompletedEventArgs</span><span style="color: #000000; padding: 0px; margin: 0px;"> es</span><span style="color: #999900; padding: 0px; margin: 0px;">)</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #999900; padding: 0px; margin: 0px;">{</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #008200; padding: 0px; margin: 0px;">// when the request has been completed, we want to bind the data to the grid.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #008200; padding: 0px; margin: 0px;">// the Result property of the EventArgs contains the returned data.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #008200; padding: 0px; margin: 0px;">// ObservableCollection is a common collection that is used when databinding to</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #008200; padding: 0px; margin: 0px;">//    a DataGrid.</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />                </span><span style="color: #990099; padding: 0px; margin: 0px;">ObservableCollection</span><span style="color: #646464; padding: 0px; margin: 0px;">&lt;string&gt;</span><span style="color: #000000; padding: 0px; margin: 0px;"> myList </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> es</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">Result</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />                dataGridPerson</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">DataContext</span><span style="color: #000000; padding: 0px; margin: 0px;"> </span><span style="color: #999900; padding: 0px; margin: 0px;">=</span><span style="color: #000000; padding: 0px; margin: 0px;"> myList</span><span style="color: #999900; padding: 0px; margin: 0px;">;</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />            </span><span style="color: #999900; padding: 0px; margin: 0px;">};</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />            client</span><span style="color: #999900; padding: 0px; margin: 0px;">.</span><span style="color: #990099; padding: 0px; margin: 0px;">GetNamesAsync</span><span style="color: #999900; padding: 0px; margin: 0px;">();</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />        </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" />    </span><span style="color: #999900; padding: 0px; margin: 0px;">}</span><span style="color: #000000; padding: 0px; margin: 0px;"><br style="padding: 0px; margin: 0px;" /></span><span style="color: #999900; padding: 0px; margin: 0px;">}</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-connecting-remote-database-wcf/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

