title: How to enable the SelectionChanged event on specific button click in Listbox in windows phone 7?
body: <p>I am developing window phone 7 application in C#. I am new to the window phone 7 application. I am also new to the silverlight. I have taken the ListBox control in my xaml file &amp; adding the many button controls to this listbox control dynamically. My xaml code is as follows</p>
  
  <pre><code>&lt;phone:PhoneApplicationPage 
      x:Class="ExpenseMgrMobAppl.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
      xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
      mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
      FontFamily="{StaticResource PhoneFontFamilyNormal}"
      FontSize="{StaticResource PhoneFontSizeNormal}"
      Foreground="{StaticResource PhoneForegroundBrush}"
      SupportedOrientations="Portrait" Orientation="Portrait"
      shell:SystemTray.IsVisible="True"&gt;
  
      &lt;!--LayoutRoot is the root grid where all page content is placed--&gt;
      &lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;!--TitlePanel contains the name of the application and page title--&gt;
          &lt;StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"&gt;
              &lt;TextBlock x:Name="ApplicationTitle" Text="Expnse Manager" Style="{StaticResource PhoneTextNormalStyle}"/&gt;
              &lt;TextBlock x:Name="PageTitle" Text="Entry Details" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/&gt;
          &lt;/StackPanel&gt;
  
          &lt;!--ContentPanel - place additional content here--&gt;
          &lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt; 
  
              &lt;toolkit:DatePicker Name="EntryDate" ValueChanged="DatePicker_ValueChanged" Margin="267,0,0,552" /&gt;
  
              &lt;ListBox x:Name="lstButtons" Margin="358,61,10,31" SelectionChanged="lstButtons_SelectionChanged" Width="80" Height="500"&gt;
                  &lt;ListBox.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;Button Content="{Binding ElementName}" Width="50" HorizontalAlignment="Center"&gt;&lt;/Button&gt;
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListBox.ItemTemplate&gt;
              &lt;/ListBox&gt;
  
              &lt;Button Content="Add" Height="72" HorizontalAlignment="Left" Margin="107,504,0,0" Name="Addbutton" VerticalAlignment="Top" Width="160" Click="Addbutton_Click" /&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  
      &lt;!--Sample code showing usage of ApplicationBar--&gt;
      &lt;!--&lt;phone:PhoneApplicationPage.ApplicationBar&gt;
          &lt;shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"&gt;
              &lt;shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/&gt;
              &lt;shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/&gt;
              &lt;shell:ApplicationBar.MenuItems&gt;
                  &lt;shell:ApplicationBarMenuItem Text="MenuItem 1"/&gt;
                  &lt;shell:ApplicationBarMenuItem Text="MenuItem 2"/&gt;
              &lt;/shell:ApplicationBar.MenuItems&gt;
          &lt;/shell:ApplicationBar&gt;
      &lt;/phone:PhoneApplicationPage.ApplicationBar&gt;--&gt;
  
  &lt;/phone:PhoneApplicationPage&gt;
  </code></pre>
  
  <p>In the above code everything is working file. Only the problem is that I am not able fire the method SelectionChanged="lstButtons_SelectionChanged" . I am able to fire this method by clicking on the edge of area of the listbox control. When I click on the middle of the square of the button, the method does not fire. But when I click outside the area of the square of the button (the area which is in the right direction of the button &amp; that area is near the edge of the area of listbox control) the method gets fired. This is I think because I am  not clicking on the right area. I think I am not able to specify the right are for button click in my code &amp; thats why the event is not firing. </p>
  
  <p>I have another example of similar type where everything is working fine &amp; event is also firing properly. The xaml for this example is as follows</p>
  
  <pre><code>&lt;phone:PhoneApplicationPage 
      x:Class="WindowsPhoneNavigation.Views.Pictures.Default"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
      xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      FontFamily="{StaticResource PhoneFontFamilyNormal}"
      FontSize="{StaticResource PhoneFontSizeNormal}"
      Foreground="{StaticResource PhoneForegroundBrush}"
      SupportedOrientations="Portrait" Orientation="Portrait"
      mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
      shell:SystemTray.IsVisible="True"&gt;
  
      &lt;!--LayoutRoot is the root grid where all page content is placed--&gt;
      &lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;!--TitlePanel contains the name of the application and page title--&gt;
          &lt;StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"&gt;
              &lt;TextBlock Text="{StaticResource AppName}" Style="{StaticResource PhoneTextNormalStyle}"/&gt;
              &lt;TextBlock Text="Images" Style="{StaticResource PhoneTextTitle1Style}"/&gt;
          &lt;/StackPanel&gt;
  
          &lt;!--ContentPanel - place additional content here--&gt;
          &lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt;
              &lt;StackPanel Orientation="Vertical"&gt;
                  &lt;StackPanel Orientation="Horizontal" Margin="5"&gt;
                      &lt;Button x:Name="btnRemoveSelection" Content="Remove Image" Click="btnRemoveSelection_Click" IsEnabled="False"/&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;ListBox x:Name="lstPictures" Width="450" Height="520" Margin="10" SelectionChanged="lstPictures_SelectionChanged"&gt;
                      &lt;ListBox.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Image Source="{Binding Image}" Width="100" Stretch="Uniform" HorizontalAlignment="Center"/&gt;
                                  &lt;TextBlock Text="{Binding Filename}" TextWrapping="Wrap" /&gt;
                              &lt;/StackPanel&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListBox.ItemTemplate&gt;
                  &lt;/ListBox&gt;
              &lt;/StackPanel&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  
      &lt;!--Sample code showing usage of ApplicationBar--&gt;
      &lt;!--&lt;phone:PhoneApplicationPage.ApplicationBar&gt;
          &lt;shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"&gt;
              &lt;shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/&gt;
              &lt;shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/&gt;
              &lt;shell:ApplicationBar.MenuItems&gt;
                  &lt;shell:ApplicationBarMenuItem Text="MenuItem 1"/&gt;
                  &lt;shell:ApplicationBarMenuItem Text="MenuItem 2"/&gt;
              &lt;/shell:ApplicationBar.MenuItems&gt;
          &lt;/shell:ApplicationBar&gt;
      &lt;/phone:PhoneApplicationPage.ApplicationBar&gt;--&gt;
  
  &lt;/phone:PhoneApplicationPage&gt;
  </code></pre>
  
  <p>Can you please tell me where I am going wrong in the first case described above ? Can you please provide me any code or link through which I can resolve the above issue. What modifcation should I done in my code for the first case described above so that when I click on exactly in the square of the button area the event SelectionChanged="lstButtons_SelectionChanged" gets fired ?</p>