<?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; Silverlight</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/silverlight/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>Silverlight 4 &#8211; Toolkit and Theming</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-4-toolkit-theming/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-4-toolkit-theming/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 22:00:46 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[silverlight toolkit]]></category>
		<category><![CDATA[silverlight4]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=615</guid>
		<description><![CDATA[I recently wanted to add a theme from the Silverlight 4 Toolkit, and ran into this runtime error&#8230; XamlParseException: Failed to create a &#8216;System.Type&#8217; from the text &#8216;input:AutoCompleteBox&#8217;. I was perplexed as I didn&#8217;t have an AutoCompleteBox in my project.  It turns out that when you drag a theme from the Toolbox onto the designer, [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wanted to add a theme from the <a href="http://silverlight.codeplex.com/" target="_blank">Silverlight 4 Toolkit</a>, and ran into this runtime error&#8230;</p>
<p><img class="alignnone size-full wp-image-617" title="12212010122804pm" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/12/12212010122804pm.png" alt="12212010122804pm" width="453" height="253" /></p>
<p><span style="color: #ff0000;"><strong>XamlParseException: Failed to create a &#8216;System.Type&#8217; from the text &#8216;input:AutoCompleteBox&#8217;.</strong></span></p>
<p>I was perplexed as I didn&#8217;t have an AutoCompleteBox in my project.  It turns out that when you drag a theme from the Toolbox onto the designer, VS2010 doesn&#8217;t automatically add all of the references needed(it will add 2).  If you drag a theme from the Toolbox into XAML, VS2010 won&#8217;t add ANY references.</p>
<p>It also seems that because themes encompass a majority of controls, there must be a reference for those controls in the project.  After much trial and error, here are the 8 references that will be needed when using a theme from the toolkit:</p>
<p><img class="alignnone size-full wp-image-618" title="References" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/12/References.png" alt="References" width="359" height="145" /></p>
<p>If I removed any of these references, the application would throw a runtime exception similar to the screenshot above.</p>
<p>The &#8220;BureauBlack&#8221; reference will change depending on the theme that you choose to use in your application.  There is a reference for every theme in the toolkit.</p>
<p>Hope this help out anybody who runs across this problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-4-toolkit-theming/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 &#8211; Productivity Power Tools and EF4</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-4-productivity-power-tools-ef4/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-4-productivity-power-tools-ef4/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 22:00:21 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ef4]]></category>
		<category><![CDATA[entity framework]]></category>
		<category><![CDATA[productivity power tools]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight 4]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=613</guid>
		<description><![CDATA[Recently, I wanted to do a project in Silverlight 4 using EF4(Entity Framework 4).  When I went to add the ADO.Net Entity Data Model class, I received this error message: The project&#8217;s target framework does not contain Entity Framework runtime assemblies. Please review the target framework information in the project&#8217;s property page. I checked my [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I wanted to do a project in Silverlight 4 using EF4(Entity Framework 4).  When I went to add the ADO.Net Entity Data Model class, I received this error message:</p>
<h3><strong>The project&#8217;s target framework does not contain Entity Framework runtime assemblies. Please review the target framework information in the project&#8217;s property page.</strong></h3>
<p>I checked my .Net framework version, and it was .Net 4.0.  So I went searching on Google and found <a href="http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/cbfab56d-3b28-4cd5-94b6-ba94ff90c0e4" target="_blank">this thread on MSDN</a>.  It seems that there is a problem with VS2010/Productivity Power Tools when using EF4.</p>
<p>If you run across this problem, here are the steps for you to take:</p>
<p>1.  Change the target framework to .Net 3.5.<br />
2.  Rebuild the project.<br />
3.  Change the target framework back to .Net 4.0.<br />
4.  Rebuild the project.</p>
<p>You should now be able to add the EF4 model class to your project.</p>
<p>Another option would be to disable the Productivity Power Tools extension, but if you are like me, the extension is a great extension and I wanted to keep it.</p>
<p>Hope this helps out anybody in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-4-productivity-power-tools-ef4/feed/</wfw:commentRss>
		<slash:comments>12</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>Silverlight 4 &#8211; Sample Data with Blend 4</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-4-sample-data-blend-4/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-4-sample-data-blend-4/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 23:00:45 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[blend]]></category>
		<category><![CDATA[blend 4]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[Silverlight 4]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=496</guid>
		<description><![CDATA[In a previous tutorial, I showed how to do a custom Item Template for a ListBox. However, one of the painstaking parts of that was having to create the class and the code to populate the ListBox with actual data. Also, even though I was populating the ListBox, I still simply saw blank controls during [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a title="Silverlight – Custom ListBox Item Template" href="http://eclipsed4utoo.com/blog/silverlightwpf-custom-listbox-item-template/" target="_blank">previous tutorial</a>, I showed how to do a custom Item Template for a ListBox. However, one of the painstaking parts of that was having to create the class and the code to populate the ListBox with actual data. Also, even though I was populating the ListBox, I still simply saw blank controls during design-time.</p>
<p>This is where Sample Data comes in. Not only can you quickly create sample data for a control, you can drag and drop it onto the control, and the ItemTemplate will be created for you(which you can still edit). And the biggest plus to using Sample Data?&#8230;.the data can be view at DESIGN-TIME.</p>
<p>So first, let&#8217;s create a Silverlight project in Blend. I named mine <strong>SampleDataProject</strong>.</p>
<p><img class="alignnone size-full wp-image-497" title="10-24-2010 9-43-51 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-43-51-AM.png" alt="10-24-2010 9-43-51 AM" width="308" height="283" /></p>
<p>Next, we will put a <strong>ListBox </strong>on our page, and give it a <strong>Height </strong>of <strong>200 </strong>and a <strong>Width </strong>of <strong>250</strong>.</p>
<pre class="brush: csharp;">
&lt;ListBox
	Margin=&quot;178,128,212,152&quot;
	Width=&quot;250&quot;
	Height=&quot;200&quot;/&gt;
</pre>
<p>Now that we have the ListBox on the page, we can create our Sample Data. Click on the <strong>Data </strong>window, then on the<strong> &#8220;Create sample data&#8221;</strong> button, then choose <strong>&#8220;New Sample Data&#8230;&#8221;</strong>&#8230;</p>
<p><img class="alignnone size-full wp-image-498" title="10-24-2010 9-47-27 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-47-27-AM.png" alt="10-24-2010 9-47-27 AM" width="238" height="146" /></p>
<p>We are going to name it <strong>PersonDataSource</strong>.</p>
<p><img class="alignnone size-full wp-image-499" title="10-24-2010 9-49-06 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-49-06-AM.png" alt="10-24-2010 9-49-06 AM" width="363" height="176" /></p>
<p>A couple of things to note on this screen. You will see two radio buttons: Project, and This document. If you choose Project, it will make the sample data available to any control in the project. If you choose This document, then the sample data will only be available to this page.</p>
<p>You will also see a checkbox on this screen saying &#8220;Enable sample data when application is running&#8221;. This allows you to use the sample data in design time, but use another data source during runtime. Uncheck this box if you want to use a different data source at runtime. Since we want our sample data for design time and runtime, we will leave it checked.</p>
<p>After creating the sample data, the Data window will now show our data in a treeview type layout. By default, it will add two properties for us.</p>
<p>Let&#8217;s change the first property by double clicking it and giving it the name <strong>FullName</strong>.</p>
<p>Let&#8217;s change the second property to <strong>Age</strong>. By default, this property is set to a boolean type. Since our age is not a boolean value, we need to change that to a numeric format. Click on the <strong>&#8220;Change property type&#8221;</strong> button on the far right of the property line. You will see a dropdown with different types. Choose <strong>&#8220;Number&#8221;</strong>. We will leave it&#8217;s <strong>length </strong>at <strong>2</strong>.</p>
<p><img class="alignnone size-full wp-image-500" title="10-24-2010 9-55-17 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-55-17-AM.png" alt="10-24-2010 9-55-17 AM" width="232" height="198" /></p>
<p>Now that we have the properties setup, we need to add some actual data. Click on the <strong>&#8220;Edit sample values&#8221;</strong> button&#8230;</p>
<p><img class="alignnone size-full wp-image-501" title="10-24-2010 9-58-49 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-58-49-AM.png" alt="10-24-2010 9-58-49 AM" width="229" height="199" /></p>
<p>You will now see a window with a number of rows and data. At the bottom, you can determine how many rows you want to show. We are going to change this to <strong>3</strong>. We are going to add some data to those rows, then click <strong>OK</strong>.</p>
<p><img class="alignnone size-full wp-image-502" title="10-24-2010 10-00-41 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-10-00-41-AM.png" alt="10-24-2010 10-00-41 AM" width="478" height="326" /></p>
<p>Now that we have our sample data setup, let&#8217;s simply drag and drop the &#8220;Collection&#8221; from the Data window onto the ListBox. The ListBox will create it&#8217;s own ItemTemplate to display the data.</p>
<p>That&#8217;s all you have to do. As you can see, we now have data in our ListBox while designing it&#8230;</p>
<p><img class="alignnone size-full wp-image-503" title="10-24-2010 10-11-16 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-10-11-16-AM.png" alt="10-24-2010 10-11-16 AM" width="429" height="350" /></p>
<p>You can look at my other tutorial on how to edit the created ItemTemplate to show the data different ways.</p>
<p>To me, this is one of the best features of Blend. Being able to see how a collection control will look at design time will speed up the development process. No longer will you have to continuously run the application to see if you have the layout correct.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-4-sample-data-blend-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Silverlight &#8211; Custom ListBox Item Template</title>
		<link>http://eclipsed4utoo.com/blog/silverlightwpf-custom-listbox-item-template/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlightwpf-custom-listbox-item-template/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 22:30:40 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[blend 4]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=475</guid>
		<description><![CDATA[In this tutorial, I will show how to create a custom ListBox Item Template. For anybody who has tried doing a custom Item Template in WinForms, you would know that it is not fun. You have to override Draw/Paint events, and other jazz to get a ListBox to show how you want it to. However, [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, I will show how to create a custom ListBox Item Template.</p>
<p>For anybody who has tried doing a custom Item Template in WinForms, you would know that it is not fun. You have to override Draw/Paint events, and other jazz to get a ListBox to show how you want it to. However, in Silverlight/WPF, this process is so much easier.</p>
<p>I will be using Blend 4 in my example, as Blend makes the process even more easier.</p>
<p>So first we are going to create a Silverlight project. I will name it <strong>CustomListBoxItemTemplate</strong>.</p>
<p><img class="alignnone size-full wp-image-485" title="10-24-2010 8-46-18 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-8-46-18-AM.png" alt="10-24-2010 8-46-18 AM" width="550" height="505" /></p>
<p>Next, we are going to add a ListBox to our page. I made mine with a <strong>Height </strong>of <strong>250</strong>, and a <strong>Width </strong>of <strong>300</strong>, and gave it the name <strong>lstPeople</strong>.</p>
<pre class="brush: csharp;">
&lt;ListBox
   x:Name=&quot;lstPeople&quot;
   ItemsSource=&quot;{Binding}&quot;
   DisplayMemberPath=&quot;FullName&quot;
   Margin=&quot;145,92,195,138&quot;/&gt;
</pre>
<p>Before we get to styling the ListBox, we need to create some test data to load into our ListBox. I created a simply Person class.</p>
<pre class="brush: csharp;">
public class Person
{
    // Insert code required on object creation below this point.
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string FullName { get { return FirstName + &quot; &quot; + LastName; } }

    public Person(string _fName, string _lName, int _age)
    {
        FirstName = _fName;
        LastName = _lName;
        Age = _age;
    }
}
</pre>
<p>And add it to the ListBox&#8230;</p>
<pre class="brush: csharp;">
public partial class MainPage : UserControl
{
	public MainPage()
	{
	     // Required to initialize variables
	     InitializeComponent();

             ObservableCollection&lt;Person&gt; peopleList = new ObservableCollection&lt;Person&gt;();
             peopleList.Add(new Person(&quot;John&quot;, &quot;Doe&quot;, 32));
             peopleList.Add(new Person(&quot;Jane&quot;, &quot;Doe&quot;, 20));
             peopleList.Add(new Person(&quot;Betty&quot;, &quot;Sue&quot;, 65));

             lstPeople.ItemsSource = peopleList;
	}
}
</pre>
<p>So now, our <strong>ListBox </strong>looks like this&#8230;</p>
<p><img class="alignnone size-full wp-image-486" title="10-24-2010 9-12-12 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-12-12-AM.png" alt="10-24-2010 9-12-12 AM" width="349" height="296" /></p>
<p>Let&#8217;s say that we want to display the age below the Full Name. We can use Blend to edit the Item Template of the <strong>ListBox</strong>.</p>
<p>In the Objects And Timeline window, <strong>right-click</strong> on the <strong>ListBox </strong>&#8211;&gt; <strong>Edit Additional Templates</strong> &#8211;&gt; <strong>Edit Generated Items(ItemTemplate)</strong> &#8211;&gt; <strong>Create Empty</strong>. You could also use the <strong>&#8220;Edit A Copy&#8221;</strong> if it&#8217;s available.</p>
<p><img class="alignnone size-full wp-image-487" title="10-24-2010 9-16-07 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-16-07-AM.png" alt="10-24-2010 9-16-07 AM" width="694" height="413" /></p>
<p>We will name it <strong>MyListBoxItemTemplate</strong>.</p>
<p><img class="alignnone size-full wp-image-488" title="10-24-2010 9-16-54 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-16-54-AM.png" alt="10-24-2010 9-16-54 AM" width="458" height="248" /></p>
<p>If you switch over to the XAML view, you will see that the <strong>ItemTemplate </strong>of the <strong>ListBox </strong>has now been binded to a <strong>Static Resource</strong>. Right above the <strong>LayoutRoot </strong>in the <strong>UserControl.Resources</strong>, you should a <strong>DataTemplate</strong>.</p>
<p>You will now see a small <strong>Grid </strong>on the screen. We will expand the grid to a <strong>Height </strong>of <strong>55 </strong>and a <strong>Width </strong>of <strong>225</strong>. Inside the grid, we are going to add 4 <strong>TextBlocks</strong>. Two will be for display purposes and the other two will show our information. Notice I am also Binding the <strong>FullName </strong>and <strong>Age </strong>properties to the <strong>Text </strong>property of the <strong>TextBlock</strong>.</p>
<pre class="brush: csharp;">&lt;Grid Width=&quot;225&quot; Height=&quot;55&quot;&gt;

	&lt;TextBlock
		Text=&quot;Full Name:&quot;
		HorizontalAlignment=&quot;Left&quot;
		Height=&quot;15&quot;
		Margin=&quot;12,8,0,0&quot;
		TextWrapping=&quot;Wrap&quot;
		VerticalAlignment=&quot;Top&quot;
		Width=&quot;70&quot;/&gt;

	&lt;TextBlock
		Text=&quot;Age:&quot;
		HorizontalAlignment=&quot;Left&quot;
		Margin=&quot;46,27,0,13&quot;
		TextWrapping=&quot;Wrap&quot;
		Width=&quot;33&quot;/&gt;

	&lt;TextBlock
		x:Name=&quot;FullNameTextBlock&quot;
		Text=&quot;{Binding FullName}&quot;
		Height=&quot;15&quot;
		Margin=&quot;86,8,8,0&quot;
		TextWrapping=&quot;Wrap&quot;
		VerticalAlignment=&quot;Top&quot; /&gt;

	&lt;TextBlock
		x:Name=&quot;AgeTextBlock&quot;
		Text=&quot;{Binding Age}&quot;
		Margin=&quot;86,27,92,13&quot;
		TextWrapping=&quot;Wrap&quot; /&gt;

&lt;/Grid&gt;</pre>
<p>And that&#8217;s all we need to do. To get out of the ItemTemplate editing, click on the ListBox at the top&#8230;</p>
<p><img class="alignnone size-full wp-image-489" title="10-24-2010 9-32-30 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-32-30-AM.png" alt="10-24-2010 9-32-30 AM" width="637" height="241" /></p>
<p>Now run the application, and your ListBox will now look like this&#8230;</p>
<p><img class="alignnone size-full wp-image-490" title="10-24-2010 9-32-45 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/10/10-24-2010-9-32-45-AM.png" alt="10-24-2010 9-32-45 AM" width="377" height="316" /></p>
<p>As you can see, using Blend in a Silverlight or WPF application, you can customize a ListBox in only a few minutes.</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;ListBox</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>x:Name=&#8221;lstPeople&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>ItemsSource=&#8221;{Binding}&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>DisplayMemberPath=&#8221;FullName&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>Margin=&#8221;145,92,195,138&#8243;/&gt;</div>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlightwpf-custom-listbox-item-template/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 &#8211; Using The Visual State Manager And Behaviors</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-4-visual-state-manager-behaviors/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-4-visual-state-manager-behaviors/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 14:57:26 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[blend 4]]></category>
		<category><![CDATA[Silverlight 4]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=449</guid>
		<description><![CDATA[In this tutorial, I am going to use the Visual State Manager and Behaviors to do animations. I will be using Blend 4 to do this tutorial. Blend will make this much easier to do rather than writing the XAML by hand. The Visual State Manager allows you to create &#8220;states&#8221; of objects and then [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #c0c0c0;">In this tutorial, I am going to use the Visual State Manager and Behaviors to do animations. I will be using Blend 4 to do this tutorial. Blend will make this much easier to do rather than writing the XAML by hand.</span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;"><br style="padding: 0px; margin: 0px;" />The Visual State Manager allows you to create &#8220;states&#8221; of objects and then move to those states. Moving to those states can be done through code(just one line of code), or by using behaviors.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" /></span><strong><span style="color: #c0c0c0;">NOTE:</span></strong><span style="color: #c0c0c0;"> The VisualStateManager was added in Silverlight 2. However, it wasn&#8217;t included in WPF until .Net 4. To use the VisualStateManager in WPF in .Net 3.5 SP1, you will need to download the WPF Toolkit from CodePlex.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />So first, we will create a new project in Blend. We will just create a Silverlight 4 application without the website. We will name the project </span><strong><span style="color: #c0c0c0;">VisualStateExample</span></strong><span style="color: #c0c0c0;">.</span></span></p>
<p><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-450" title="9-12-2010 9-36-57 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-36-57-AM.png" alt="9-12-2010 9-36-57 AM" width="550" height="505" /></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">We will now draw a </span><strong><span style="color: #c0c0c0;">Rectangle</span></strong><span style="color: #c0c0c0;"> on our page, and give it a </span><strong><span style="color: #c0c0c0;">Fill </span></strong><span style="color: #c0c0c0;">of </span><strong><span style="color: #c0c0c0;">Blue</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-451" title="9-12-2010 9-39-35 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-39-35-AM.png" alt="9-12-2010 9-39-35 AM" width="58" height="323" /> <img class="alignnone size-full wp-image-453" title="9-12-2010 9-42-18 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-42-18-AM.png" alt="9-12-2010 9-42-18 AM" width="281" height="480" /> </span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><br />
<img class="alignnone size-full wp-image-452" title="9-12-2010 9-41-19 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-41-19-AM.png" alt="9-12-2010 9-41-19 AM" width="487" height="283" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">Next, we are going to go to the </span><strong><span style="color: #c0c0c0;">States </span></strong><span style="color: #c0c0c0;">tab.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-454" title="9-12-2010 9-44-56 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-44-56-AM.png" alt="9-12-2010 9-44-56 AM" width="363" height="458" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">This is where we will create all of our states.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />So first, we need to create a State Group. Click the </span><strong><span style="color: #c0c0c0;">Add State Group</span></strong><span style="color: #c0c0c0;"> button, name it </span><strong><span style="color: #c0c0c0;">MyStateGroup</span></strong><span style="color: #c0c0c0;">, then give it a default transition time of </span><strong><span style="color: #c0c0c0;">0.4s</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-455" title="9-12-2010 9-47-41 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-47-41-AM.png" alt="9-12-2010 9-47-41 AM" width="357" height="299" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-456" title="9-12-2010 9-48-37 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-48-37-AM.png" alt="9-12-2010 9-48-37 AM" width="365" height="333" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">Now that we have our State Group, we need to create our individual states. Let&#8217;s say that we want the rectangle to change colors when the mouse hovers over it. When the mouse moves away, we want it to change back to it&#8217;s original color. Also, let&#8217;s change the color when the left mouse button is held down on the rectangle(Mouse Down), then change back when the left mouse button is released(Mouse Up).<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />So let&#8217;s create our first state. Our first state will be our current, default state. Click the </span><strong><span style="color: #c0c0c0;">Add State </span></strong><span style="color: #c0c0c0;">button, then name it </span><strong><span style="color: #c0c0c0;">MouseOutState</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-456" title="9-12-2010 9-48-37 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-48-37-AM.png" alt="9-12-2010 9-48-37 AM" width="365" height="333" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-457" title="9-12-2010 9-55-30 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-55-30-AM.png" alt="9-12-2010 9-55-30 AM" width="402" height="301" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">If you notice, now there is a red rectangle around the designer. This means that you are currently in recording mode for the state. ANY changes that are made in the designer will be recorded for that state. Clicking on the </span><strong><span style="color: #c0c0c0;">Base </span></strong><span style="color: #c0c0c0;">state will stop recording.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />Since this is our default state, we don&#8217;t need to record anything.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />Now, create another state named </span><strong><span style="color: #c0c0c0;">MouseOverState</span></strong><span style="color: #c0c0c0;">. While still in recording mode, select the </span><strong><span style="color: #c0c0c0;">Rectangle </span></strong><span style="color: #c0c0c0;">from the </span><strong><span style="color: #c0c0c0;">Objects and Timeline</span></strong><span style="color: #c0c0c0;"> tab, and change it&#8217;s </span><strong><span style="color: #c0c0c0;">Fill</span></strong><span style="color: #c0c0c0;"> color to </span><strong><span style="color: #c0c0c0;">Orange</span></strong><span style="color: #c0c0c0;">. Click on the </span><strong><span style="color: #c0c0c0;">Base</span></strong><span style="color: #c0c0c0;"> state to stop recording.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-458" title="9-12-2010 9-58-42 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-9-58-42-AM.png" alt="9-12-2010 9-58-42 AM" width="380" height="376" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-459" title="9-12-2010 10-01-16 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-01-16-AM.png" alt="9-12-2010 10-01-16 AM" width="360" height="331" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-460" title="9-12-2010 10-02-30 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-02-30-AM.png" alt="9-12-2010 10-02-30 AM" width="288" height="479" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">Now, create another state named </span><strong><span style="color: #c0c0c0;">MouseDownState</span></strong><span style="color: #c0c0c0;">. While still in recording mode, change it&#8217;s </span><strong><span style="color: #c0c0c0;">Fill </span></strong><span style="color: #c0c0c0;">color to </span><strong><span style="color: #c0c0c0;">Green</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-461" title="9-12-2010 10-09-08 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-09-08-AM.png" alt="9-12-2010 10-09-08 AM" width="368" height="284" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-462" title="9-12-2010 10-09-33 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-09-33-AM.png" alt="9-12-2010 10-09-33 AM" width="270" height="455" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">Now, we need to create our last state. Create a new state and name it </span><strong><span style="color: #c0c0c0;">MouseUpState</span></strong><span style="color: #c0c0c0;">. While still in recording mode, change the </span><strong><span style="color: #c0c0c0;">Fill</span></strong><span style="color: #c0c0c0;"> color to the </span><strong><span style="color: #c0c0c0;">Orange</span></strong><span style="color: #c0c0c0;"> from the </span><strong><span style="color: #c0c0c0;">MouseOverState</span></strong><span style="color: #c0c0c0;">.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />We should now have 4 states. Now we need to go to these states with the mouse events. There are a couple of ways of doing this, but I will use </span><strong><span style="color: #c0c0c0;">behaviors</span></strong><span style="color: #c0c0c0;"> to do it.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />There are a number of behaviors in Blend, and we are going to use the </span><strong><span style="color: #c0c0c0;">GoToStateAction </span></strong><span style="color: #c0c0c0;">behavior.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />Go to the </span><strong><span style="color: #c0c0c0;">Assets</span></strong><span style="color: #c0c0c0;"> tab, then click on </span><strong><span style="color: #c0c0c0;">behaviors</span></strong><span style="color: #c0c0c0;">, then click on the </span><strong><span style="color: #c0c0c0;">GoToStateAction</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-463" title="9-12-2010 10-15-22 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-15-22-AM.png" alt="9-12-2010 10-15-22 AM" width="366" height="490" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">Now drag that action onto the Rectange in the designer. You will now see that the </span><strong><span style="color: #c0c0c0;">GoToStateAction</span></strong><span style="color: #c0c0c0;"> has been added to the Rectangle in the </span><strong><span style="color: #c0c0c0;">Objects and Timeline</span></strong><span style="color: #c0c0c0;"> tab.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-464" title="9-12-2010 10-19-13 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-19-13-AM.png" alt="9-12-2010 10-19-13 AM" width="348" height="385" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">With the </span><strong><span style="color: #c0c0c0;">GoToStateAction</span></strong><span style="color: #c0c0c0;"> selected, go to the </span><strong><span style="color: #c0c0c0;">Properties</span></strong><span style="color: #c0c0c0;"> tab, set the </span><strong><span style="color: #c0c0c0;">EventName</span></strong><span style="color: #c0c0c0;"> to </span><strong><span style="color: #c0c0c0;">MouseEnter</span></strong><span style="color: #c0c0c0;">, and set </span><strong><span style="color: #c0c0c0;">StateName</span></strong><span style="color: #c0c0c0;"> to </span><strong><span style="color: #c0c0c0;">MouseOverState</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-465" title="9-12-2010 10-20-29 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-20-29-AM.png" alt="9-12-2010 10-20-29 AM" width="286" height="418" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="color: #c0c0c0;">Do these steps to create a </span><strong><span style="color: #c0c0c0;">GoToStateAction</span></strong><span style="color: #c0c0c0;"> for the other three states that we have. When you have completed, you will have four actions for the </span><strong><span style="color: #c0c0c0;">Rectangle</span></strong><span style="color: #c0c0c0;">.</span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;"><img class="alignnone size-full wp-image-466" title="9-12-2010 10-23-54 AM" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/09/9-12-2010-10-23-54-AM.png" alt="9-12-2010 10-23-54 AM" width="370" height="353" /></span></span></span></p>
<p><span style="font-family: Verdana, sans-serif; line-height: 21px; font-size: 14px; color: #222222;"><span style="line-height: 21px; font-size: 14px;"><span style="color: #c0c0c0;">You have now finished the animations. Run the application using F5, and move the mouse over the rectangle. Perform the other mouse actions to see the other animations.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />This is just another way the Exp<strong></strong>ression Blend makes animations so easy. I have created animations without writing one line of code. To me, that&#8217;s pretty amazing and can make application development much quicker and have less bugs.</span></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-4-visual-state-manager-behaviors/feed/</wfw:commentRss>
		<slash:comments>4</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>Silverlight 4 &#8211; Creating a Custom Modal Dialog</title>
		<link>http://eclipsed4utoo.com/blog/silverlight-4-creating-custom-modal-dialog/</link>
		<comments>http://eclipsed4utoo.com/blog/silverlight-4-creating-custom-modal-dialog/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 06:30:26 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[blend]]></category>
		<category><![CDATA[modal dialog]]></category>
		<category><![CDATA[Silverlight 4]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=380</guid>
		<description><![CDATA[For a Silverlight application I did recently, I wanted to do a custom modal dialog box for showing informational messages, error messages, or other messages to the user.  This dialog box would have no title bar or &#8220;X&#8221; button, and would have rounded corners. For this tutorial, we will use Blend 4 to accomplish our [...]]]></description>
			<content:encoded><![CDATA[<p>For a Silverlight application I did recently, I wanted to do a custom modal dialog box for showing informational messages, error messages, or other messages to the user.  This dialog box would have no title bar or &#8220;X&#8221; button, and would have rounded corners.</p>
<p>For this tutorial, we will use Blend 4 to accomplish our task.</p>
<p>First, create a new Silverlight 4 application.  I named mine <strong>CustomDialog</strong>.</p>
<p><img class="alignnone size-full wp-image-381" title="Image1" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image1.png" alt="Image1" width="548" height="468" /></p>
<p>Once the project has been created, we are going to add a new item to our Silverlight project.  This item will be a <strong>ChildWindow</strong>.  I named mine <strong>DialogPopup</strong>.</p>
<p><img class="alignnone size-full wp-image-382" title="Image2" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image2.png" alt="Image2" width="390" height="427" /></p>
<p>The <strong>ChildWindow </strong>will handle the modal dialog part of our window.  However, there are parts of the <strong>ChildWindow </strong>that I didn&#8217;t like and wanted to remove/change, such as the window chrome.  I also wanted to round the corners of the window.   To do this, we need to edit the template for the window.  In the <strong>Objects and Timeline</strong> window, right-click on the <strong>ChildWindow </strong>parent &#8211;&gt; <strong>Edit Template</strong> &#8211;&gt; <strong>Edit a Copy</strong>&#8230;</p>
<p><img class="alignnone size-full wp-image-383" title="Image3" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image3.png" alt="Image3" width="457" height="495" /></p>
<p>We are going to keep it simple and save the new style to the ChildWindow document.</p>
<p><img class="alignnone size-full wp-image-384" title="Image4" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image4.png" alt="Image4" width="459" height="249" /></p>
<p>So first, we are just going to delete the window chrome from the window.  The window chrome is the title bar and the &#8220;X&#8221; used to close the window.  Drill-down in the treeview until you find the <strong>Chrome </strong>entry.  Right-click, and choose <strong>Delete</strong>.</p>
<p><img class="alignnone size-full wp-image-385" title="Image5" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image5.png" alt="Image5" width="437" height="391" /></p>
<p>Next, we need to delete some of the Borders.  These give the window a grey border that I didn&#8217;t want.</p>
<p><img class="alignnone size-full wp-image-402" title="Image11" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image11.png" alt="Image11" width="431" height="373" /></p>
<p>Next, we are going to round the corners of the window.  Drill-down and click on the <strong>Border </strong>shown below.</p>
<p><img class="alignnone size-full wp-image-405" title="Image12" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image12.png" alt="Image12" width="290" height="244" /></p>
<p>In the <strong>Properties </strong>window, set the <strong>CornerRadius </strong>to <strong>20</strong>.</p>
<p><img class="alignnone size-full wp-image-387" title="Image7" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image7.png" alt="Image7" width="275" height="192" /></p>
<p>Move down to the next <strong>Border</strong>,</p>
<p><img class="alignnone size-full wp-image-406" title="Image13" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image13.png" alt="Image13" width="302" height="249" /></p>
<p>And set it&#8217;s <strong>CornerRadius </strong>to <strong>20</strong>.</p>
<p><img class="alignnone size-full wp-image-389" title="Image9" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image9.png" alt="Image9" width="271" height="177" /></p>
<p>With the same <strong>Border </strong>selected, in the Properties window, set the <strong>Background Brush</strong> to a color of your choice.</p>
<p><img class="alignnone size-full wp-image-390" title="Image10" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image10.png" alt="Image10" width="270" height="391" /></p>
<p>The window &#8220;jazz&#8221; is now complete.  Next, we are going to move to the XAML.</p>
<p>First, we need to give our window a name so we can handle binding.  I named mine <strong>myCustomPopup</strong>.</p>
<pre class="brush: xml;">
&lt;sdk:ChildWindow
   xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
   xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
   xmlns:sdk=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk&quot;
   x:Class=&quot;CustomDialog.DialogPopup&quot;
   x:Name=&quot;myCustomPopup&quot;
   Title=&quot;DialogPopup&quot;
   Width=&quot;400&quot; Height=&quot;300&quot;&gt;
</pre>
<p>Now we need to add a <strong>TextBlock </strong>to the window to hold our message.</p>
<pre class="brush: xml;">
&lt;TextBlock
    Text=&quot;{Binding Message, ElementName=myCustomPopup}&quot;
    Margin=&quot;51,88,57,113&quot;
    TextWrapping=&quot;Wrap&quot;
    Foreground=&quot;#FF1D1515&quot;
    FontWeight=&quot;Bold&quot;
    TextAlignment=&quot;Center&quot;/&gt;
</pre>
<p>You may notice that we are binding to a property called <strong>Message</strong>.  This is a custom <strong>DependencyProperty </strong>that will correspond to a public property that we will set when we create an instance of the window.</p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// Creates a public property for the TextBlock to bind to
/// &lt;/summary&gt;
public static DependencyProperty MessageProperty = DependencyProperty.Register(&quot;Message&quot;, typeof(string), typeof(DialogPopup), new PropertyMetadata(&quot;&quot;));
public string Message
{
     get { return (string)GetValue(MessageProperty); }
     set { SetValue(MessageProperty, value); }
}
</pre>
<p>This will complete our work for the window.  To show off the new window, lets add a <strong>Button </strong>to the <strong>MainPage.xaml</strong>, create an <strong>EventHandler </strong>for the <strong>Click </strong>event.</p>
<pre class="brush: xml;">
&lt;Grid
   x:Name=&quot;LayoutRoot&quot;
   Background=&quot;White&quot;&gt;

     &lt;Button
        x:Name=&quot;btnShowMessage&quot;
        Content=&quot;Click Me&quot;
        Click=&quot;btnShowMessage_Click&quot;
        Margin=&quot;251,207,289,236&quot;  /&gt;
&lt;/Grid&gt;
</pre>
<p>And the code for the <strong>Click </strong>event&#8230;</p>
<pre class="brush: csharp;">
private void btnShowMessage_Click(object sender, System.Windows.RoutedEventArgs e)
{
     DialogPopup dialog = new DialogPopup();
     dialog.Message = &quot;Our message&quot;;
     dialog.Show();
}
</pre>
<p>And that will complete our tutorial.  Run the application from Blend using <strong>F5</strong>, and you should see this..</p>
<p><img class="alignnone size-full wp-image-408" title="Image14" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2010/07/Image14.png" alt="Image14" width="489" height="364" /></p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/silverlight-4-creating-custom-modal-dialog/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

