<?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; .Net</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/net/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>C# &#8211; Get Windows Mobile Theme Color</title>
		<link>http://eclipsed4utoo.com/blog/windows-mobile-theme-color/</link>
		<comments>http://eclipsed4utoo.com/blog/windows-mobile-theme-color/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 23:00:01 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[windows mobile]]></category>

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

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

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

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

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

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

if(themeColor != null)
   txtComments.BackColor = themeColor;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/windows-mobile-theme-color/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WPF &#8211; Using Data Triggers</title>
		<link>http://eclipsed4utoo.com/blog/wpf-data-triggers/</link>
		<comments>http://eclipsed4utoo.com/blog/wpf-data-triggers/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 23:30:07 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=633</guid>
		<description><![CDATA[Data triggers are a great feature of WPF. They allow you to change the style properties of a control depending on the data of the bound items of that control.In this tutorial, I am going to bind a set of data to a ListBox, and use Data Triggers to let me know when something is [...]]]></description>
			<content:encoded><![CDATA[<p>Data triggers are a great feature of WPF. They allow you to change the style properties of a control depending on the data of the bound items of that control.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />In this tutorial, I am going to bind a set of data to a <strong>ListBox</strong>, and use Data Triggers to let me know when something is wrong.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />So first, I am going to create a <strong>Person </strong>class. This class will have three public properties: <strong>Name</strong>, <strong>Age</strong>, and <strong>IsValid</strong>. All three properties will be readonly. The <strong>IsValid </strong>property will return whether the data of the class is good or not.</p>
<p><strong>Person.cs</strong></p>
<pre class="brush: csharp;">public class Person
{
    private string name;

    public string Name
    {
        get { return name; }
        private set { name = value; }
    }
    private int age;

    public int Age
    {
        get { return age; }
        private set { age = value; }
    }

    public bool IsValid
    {
        // will return false if either the name is blank
        //   or if the age is 0.
        get { return (!string.IsNullOrEmpty(name) &amp;&amp; age != 0); }
    }

    public Person(string _name, int _age)
    {
        name = _name;
        age = _age;
    }
}</pre>
<p>This is a simple example to show the concept. Basically, I am going to set the data to values that we don&#8217;t want.</p>
<p><strong><br />
MainWindow.xaml.cs</strong></p>
<pre class="brush: csharp;">
public partial class MainWindow : Window
{
    ObservableCollection&lt;Person&gt; col;

    public MainWindow()
    {
        InitializeComponent();

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

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        col = new ObservableCollection&lt;Person&gt;();
        col.Add(new Person(&quot;John Doe&quot;, 24));
        col.Add(new Person(&quot;&quot;, 24));
        col.Add(new Person(&quot;John Doe&quot;, 0));

        PersonListBox.ItemsSource = col;
    }
}</pre>
<p>As you can see, I set the name to being blank for one object, and the age to being 0 for another object.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />Now for the XAML. First, we need to add a new style to the <strong>Window.Resources</strong>. This style will target the <strong>ListBoxItem </strong>type.</p>
<pre class="brush: csharp;">&lt;Window.Resources&gt;
    &lt;Style TargetType=&quot;{x:Type ListBoxItem}&quot; x:Key=&quot;ItemStyle&quot;&gt;
        &lt;Style.Triggers&gt;
            &lt;DataTrigger Binding=&quot;{Binding IsValid}&quot; Value=&quot;false&quot;&gt;
                &lt;Setter Property=&quot;Background&quot; Value=&quot;Red&quot; /&gt;
            &lt;/DataTrigger&gt;
        &lt;/Style.Triggers&gt;
    &lt;/Style&gt;
&lt;/Window.Resources&gt;</pre>
<p>So if our <strong>IsValid </strong>property is false, then we will set the <strong>Background </strong>property of the <strong>ListBoxItem </strong>to Red.</p>
<p>Then we have our <strong>ListBox</strong>,</p>
<pre class="brush: csharp;">
&lt;Grid&gt;
   &lt;ListBox
       Name=&quot;PersonListBox&quot;
       ItemsSource=&quot;{Binding}&quot;
       ItemContainerStyle=&quot;{StaticResource ItemStyle}&quot;
       Height=&quot;142&quot;
       HorizontalAlignment=&quot;Left&quot;
       Margin=&quot;39,30,0,0&quot;
       VerticalAlignment=&quot;Top&quot;
       Width=&quot;190&quot;&gt;

       &lt;ListBox.ItemTemplate&gt;

          &lt;DataTemplate&gt;

             &lt;StackPanel Orientation=&quot;Vertical&quot;&gt;

                 &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;

                    &lt;TextBlock
                        Text=&quot;Name:&quot;
                        Width=&quot;50&quot; /&gt;

                    &lt;TextBlock
                        Text=&quot;{Binding Name}&quot;
                        Width=&quot;150&quot;  /&gt;

                 &lt;/StackPanel&gt;

                 &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;

                    &lt;TextBlock
                       Text=&quot;Age:&quot;
                       Width=&quot;50&quot; /&gt;

                    &lt;TextBlock
                       Text=&quot;{Binding Age}&quot;
                       Width=&quot;50&quot;  /&gt;

                 &lt;/StackPanel&gt;

             &lt;/StackPanel&gt;

         &lt;/DataTemplate&gt;

     &lt;/ListBox.ItemTemplate&gt;

   &lt;/ListBox&gt;
&lt;/Grid&gt;
</pre>
<p>So I am binding the <strong>ItemsSource </strong>to the collection in the code-behind. Also notice that I am binding the style we created to the <strong>ItemContainerStyle</strong>. The <strong>DataTemplate </strong>simply creates a custom ItemTemplate to show the data.</p>
<p>So now if we run our code, we get this&#8230;</p>
<p><img class="alignnone size-full wp-image-652" title="1" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2011/03/1.png" alt="1" width="518" height="346" /></p>
<p>But that&#8217;s not all. Let&#8217;s say that we want to highlight the rows in different colors depending on if the name is blank or if the age is 0. We can create two separate triggers for that. We would simply change our style to this&#8230;</p>
<pre class="brush: csharp;">
&lt;Window.Resources&gt;
   &lt;Style TargetType=&quot;{x:Type ListBoxItem}&quot; x:Key=&quot;ItemStyle&quot;&gt;
       &lt;Style.Triggers&gt;
          &lt;DataTrigger Binding=&quot;{Binding Name}&quot; Value=&quot;&quot;&gt;
               &lt;Setter Property=&quot;Background&quot; Value=&quot;Blue&quot; /&gt;
          &lt;/DataTrigger&gt;
          &lt;DataTrigger Binding=&quot;{Binding Age}&quot; Value=&quot;0&quot;&gt;
               &lt;Setter Property=&quot;Background&quot; Value=&quot;Yellow&quot; /&gt;
          &lt;/DataTrigger&gt;
       &lt;/Style.Triggers&gt;
   &lt;/Style&gt;
&lt;/Window.Resources&gt;
</pre>
<p>With that change, our ListBox now shows this&#8230;</p>
<p><img class="alignnone size-full wp-image-653" title="2" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2011/03/2.png" alt="2" width="517" height="347" /></p>
<p>As you can see, Data Triggers are a very powerful part of WPF. Now if only Microsoft would add this to Silverlight.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/wpf-data-triggers/feed/</wfw:commentRss>
		<slash:comments>3</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>C# &#8211; Twitter API For Desktop</title>
		<link>http://eclipsed4utoo.com/blog/twitter-api-desktop/</link>
		<comments>http://eclipsed4utoo.com/blog/twitter-api-desktop/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 14:16:58 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=259</guid>
		<description><![CDATA[I made a post almost 3 months ago about a .Net Twitter API that I had done.  Since then, I have made some modifications to the API.  I have added a couple of more features.  I also included various classes from the System.Web namespace so that I wouldn&#8217;t need a reference to that namespace.  This [...]]]></description>
			<content:encoded><![CDATA[<p>I made a post almost 3 months ago about a .Net Twitter API that I had done.  Since then, I have made some modifications to the API.  I have added a couple of more features.  I also included various classes from the System.Web namespace so that I wouldn&#8217;t need a reference to that namespace.  This allows the API to be used on a mobile device with a very small footprint(the System.Web.dll file is 5MB in size, my .dll is 52KB).</p>
<p>I am missing a few features from my API.  Most notably the new &#8220;Retweet&#8221; functionality and the &#8220;Lists&#8221; functionality.  I am also missing some of the account methods.  Other than that, I believe I have most, if not all, of the other methods.</p>
<p>This API supports both Basic Auth and OAuth.  I would advise to use OAuth, since Twitter will be depreciating Basic Auth in <a href="http://www.techcrunch.com/2009/12/09/twitter-le-web-2009/">June 2010</a>.  To learn more about how OAuth works, check out my <a href="http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/">other blog post</a> where I try to explain OAuth.</p>
<p>Remember that this API is for the Desktop OAuth only.  Feel free to download it and make changes to allow OAuth from a web application(yes, they are different &#8220;workflows&#8221;).   Basic Auth should work for both Desktop and Web applications.</p>
<p>I give credit to <a href="http://www.voiceoftech.com/swhitley/?p=681">Shannon Whitley for his original OAuth code</a>.  I made some modifications to it, but the base code of the OAuth is his.</p>
<p>Here is a code snippet on using the API for OAuth authorization.</p>
<pre class="brush: csharp;">
// creats instance and sets Consumer and ConsumerSecret values
TwitEclipseAPI twit = new TwitEclipseAPI();
twit.OAuthConsumerKey = &quot;yourConsumerKey&quot;;
twit.OAuthConsumerSecret = &quot;yourConsumerSecret&quot;;

// makes request to get unauthorized request token
// The method will concatenate the request token to Twitter's Desktop OAuth
//    url (http://twitter.com/oauth/authorize)
string url = twit.OAuthGetUnauthorizedRequestToken();

// opens the user's default browser and browses to Twitter's OAuth page
Process.Start(url);

//  You will need to get the PIN from the user
//  I just created a popup and have the user enter the PIN
//       into a textbox in the popup
frmPinPopup popup = new frmPinPopup();
popup.ShowDialog();

string PIN = popup.PIN;
popup.Dispose();

// Gets the Token and TokenSecret that will be needed for all
//   subsequent requests.  You will need to save these values to keep
//   from forcing the user to authorize your application everytime they
//   open your application.
twit.OAuthRequestAccessToken(PIN);

// You can check for success by checking the OAuthAccessToken
//    and OAuthAccessTokenSecret values.  If they are populated, then
//    it was successful.  If they are empty, then it failed.
if (!string.IsNullOrEmpty(twit.OAuthAccessToken) &amp;&amp; !string.IsNullOrEmpty(twit.OAuthAccessTokenSecret))
{
    MessageBox.Show(&quot;Authorization Successful&quot;);
}
</pre>
<p>Each method of the API will check the OAuthAccessToken and OAuthAccessTokenSecret values to determine whether it needs to do the request using OAuth or Basic Auth.</p>
<p>I have put the code on <a href="http://twiteclipseapi.codeplex.com/" target="_blank">CodePlex</a>.  It will contain both the .dll file, and the source code.  I felt it was easier to keep up with using CodePlex.</p>
<p>If you have code questions, you can post here.  However, if you are on Google Wave, <a href="https://wave.google.com/wave/#restored:search:twiteclipseapi,restored:wave:googlewave.com!w%252BtKPwoLd7A.5">you can go here</a>.  If you would like Google Wave, I have about 20 invitations that I can send out.  Using Google Wave would be much better since we could exchange code using a code snippet tool.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/twitter-api-desktop/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>TwitEclipse &#8211; My Twitter Desktop Client</title>
		<link>http://eclipsed4utoo.com/blog/twiteclipse-twitter-desktop-client/</link>
		<comments>http://eclipsed4utoo.com/blog/twiteclipse-twitter-desktop-client/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 20:15:26 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[twitter desktop client]]></category>
		<category><![CDATA[wpf]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=195</guid>
		<description><![CDATA[I have released a BETA version of my Twitter Desktop Client called TwitEclipse.  I have been writing a Twitter API library in .Net for a while now, and figured I might as well write a desktop client also. The client uses .Net 3.5 SP1 and WPF.  It was a great learning experience to learn WPF [...]]]></description>
			<content:encoded><![CDATA[<p>I have released a BETA version of my Twitter Desktop Client called TwitEclipse.  I have been writing a Twitter API library in .Net for a while now, and figured I might as well write a desktop client also.</p>
<p>The client uses .Net 3.5 SP1 and WPF.  It was a great learning experience to learn WPF since I had almost no previous experience with it.</p>
<p>If you want to download it and test it out, you can download it from <a title="TwitEclipse" href="http://eclipsed4utoo.com/TwitEclipse" target="_blank">here</a>.  Remember that it is a BETA.  You could run into issues.  If you do, be sure to let me know so I can fix them.  I will also be adding additional features to the app once I get a chance.</p>
<p>I will be releasing my Twitter API library soon also.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/twiteclipse-twitter-desktop-client/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>.Net &#8211; Twitter Desktop OAuth Authorization</title>
		<link>http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/</link>
		<comments>http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 14:25:12 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=177</guid>
		<description><![CDATA[I recent finished the implementation of OAuth into my .Net Twitter library.  I can safely say that it was a pain in the ass.  I have never been so frustrated by &#8220;(401) Unauthorized&#8221; errors.  But alas, I was able to complete the implementation. As a guideline, I used Shannon Whitley&#8217;s code, since that&#8217;s the only [...]]]></description>
			<content:encoded><![CDATA[<p>I recent finished the implementation of OAuth into my .Net Twitter library.  I can safely say that it was a pain in the ass.  I have never been so frustrated by &#8220;(401) Unauthorized&#8221; errors.  But alas, I was able to complete the implementation.</p>
<p>As a guideline, I used <a href="http://www.voiceoftech.com/swhitley/?p=681">Shannon Whitley&#8217;s</a> code, since that&#8217;s the only .Net reference Twitter gives for OAuth implementation.  Since I had already created my own Twitter library that consumed 95% of the current Twitter REST API methods, I couldn&#8217;t simply add his two code files to my project and run with it.  I had to change some stuff around to get it to work with mine.  I also made some modifications to his class that makes all of the OAuth implementations.  In my opinion, it had a lot of code that didn&#8217;t need to be there.  I also created another class to consume the parameters needed for the OAuth requests so that I wasn&#8217;t passing 9 parameters to different methods.</p>
<p>So first, I want to explain OAuth a little and how it works for a desktop Twitter client.  I stress desktop application because Twitter has different implementations for web clients vs. desktop clients.</p>
<p>So <a href="http://oauth.net/">OAuth</a> is an open protocol to allow secure authorization to an API.  This means that for Twitter, the user&#8217;s of a desktop client aren&#8217;t required to give their username and password to the client.  The client can use OAuth to communicate with with the Twitter API without needing usernames and passwords.</p>
<p>Now for the process.  This was the toughest part for me to get my head around.  I couldn&#8217;t really find any good explanations of each step of the process.  So I will explain as much as I can.</p>
<p>Your first step is to <a href="http://twitter.com/oauth_clients">register your application</a> with Twitter.  This does a couple of things.  It gives you a &#8220;Consumer Key&#8221; and &#8220;Consumer Secret&#8221;.  These are needed in each OAuth request that is made.  This also provides the &#8220;source&#8221; for the tweets.  Twitter recently removed the ability to set the &#8220;source&#8221; of the tweet to your application name when using Basic Authentication.  This is now only allowed when using OAuth.</p>
<p>When making requests to OAuth methods, there are a number of parameters that are required for all requests.  And depending on the request, some may be expecting additional parameters.  The required parameters for all requests are:</p>
<p>1.  Consumer Key &#8211; given when you register your application.<br />
2.  Nonce &#8211; a random string.  I use a GUID.  Some use a random number.<br />
3.  Timestamp &#8211; the number of seconds since January 1, 1970.<br />
4.  Signature &#8211; an HMAC-SHA1 string of all of the other parameters.<br />
5.  Signature Method &#8211; HMAC-SHA1.  Currently Twitter only supports HMAC-SHA1.<br />
6.  Token &#8211; unauthorized/request/access token.<br />
7.  Verifier &#8211; the PIN from the Desktop Workflow<br />
8.  Version &#8211; &#8220;1.0&#8243;</p>
<p>When creating the signature, you will use the Consumer Secret and Token Secret as the key.  Also, the parameters that are part of the signature need to be in order when creating the signature and when making the request.  The signature is appended to the end of the parameters so it does not need to be in order.</p>
<p>Here is the order that the parameters need to be in:</p>
<p>1.  oauth_consumer_key<br />
2.  oauth_nonce<br />
3.  oauth_signature_method<br />
4.  oauth_timestamp<br />
5.  oauth_token<br />
6.  oauth_verifier<br />
7.  oauth_version<br />
8.  oauth_signature</p>
<p>To make the signature, you would use this code:</p>
<pre class="brush: csharp;">
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format(&quot;{0}&amp;{1}&quot;, UrlEncode(oauth.CustomerSecret), string.IsNullOrEmpty(oauth.TokenSecret) ? &quot;&quot; : UrlEncode(oauth.TokenSecret)));
// code to compute the hash to return as the signature
</pre>
<p>Moving to the code, your first step is to send an HttpWebRequest to http://twitter.com/oauth/request_token .  This request will return an Unauthorized Token and Unauthorized Token Secret.  In the current library that I used(and my Twitter API library), this method will return the entire URL including the token and secret, which will look like this&#8230;</p>
<p>http://twitter.com/oauth/authorize?oauth_token=UnauthorizedTokenValue</p>
<p>Your next step is to have the user go to the URL that was returned from the previous step.  This is the step that many developers have complained about.  When navigating to this URL, the user will enter their username and password.  They will then be given a PIN.  You will need to get this PIN from the user because it is required for you to get an Access Token.  I simply give a textbox for the user to copy and paste to.</p>
<p>After receiving the PIN, you will now need to make a request to http://twitter.com/oauth/access_token .  This will give you the Access Token that you will need to make all other requests to the Twitter REST API.  You will need to save these somewhere, whether it be in a database or in a file.</p>
<p>Here is some client code..</p>
<pre class="brush: csharp;">
TwitEclipseAPI twit = new TwitEclipseAPI();
twit.OAuthConsumerKey = &quot;&quot;; // your consumer key
twit.OAuthConsumerSecret = &quot;&quot;; // your consumer secret

// Once you get the access token, this
//    would be out to set it
//twit.OAuthAccessToken = &quot;&quot;;
//twit.OAuthAccessTokenSecret = &quot;&quot;;

string redirectURL = twit.OAuthGetUnauthorizedRequestToken();
Process.Start(redirectURL);

// show a popup to enter PIN
frmEnterPIN f = new frmEnterPIN();
f.ShowDialog();
string pin = f.PIN;
f.Dispose();

if (twit.OAuthRequestAccessToken(pin))
{
   twit.UpdateUserStatus(&quot;This is a test&quot;);
}
</pre>
<p>I have posted a link to the code files for my library and how it uses the edited version of Shannon&#8217;s library.</p>
<p>Post any questions in the comments.  I will answer them as best I can.  If  you run into any issues, please let me know.  Thanks.</p>
<p><strong>UPDATE:</strong> I have update the site with a new version of my twitter library.  I have also moved the download to CodePlex so that it will be easier for me to update.  <a href="http://twiteclipseapi.codeplex.com/" target="_blank">Here is the link</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/net-twitter-desktop-oauth-authentication/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

