Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
unable to access the selected image in the listbox

.xaml code:

XML
<listbox x:name="listBox" margin="10,282,0,0" grid.columnspan="2" xmlns:x="#unknown">
     
                        <listbox.itemspanel>
                        <itemspaneltemplate>
                            <wrappanel flowdirection="LeftToRight" itemwidth="120" itemheight="120" />
                        </itemspaneltemplate>
                    </listbox.itemspanel>
                    <listbox.itemtemplate>
                        <datatemplate>
                            <Button x:Name="b" Click="b_Click_1" Width="120" Height="120" >
                                <Image HorizontalAlignment="Left" 
                                Margin="-10,-10,-10,-10" 
                                x:Name="image1" 
                                Stretch="Fill" 
                                VerticalAlignment="Top" Source="{Binding}"/>
                            </Button>
                            
                        </datatemplate>
                    </listbox.itemtemplate>
                </listbox>


.cs code:

C#
  private void b_Click_1(object sender, RoutedEventArgs e)
   {
         try
       {
           MessageBoxResult a = MessageBox.Show("Are you want to upload", "Photo Upload", MessageBoxButton.OKCancel);
           if (a == MessageBoxResult.OK)
           {
System.Drawing.Image b1 = (sender as Button).DataContext as System.Drawing.Image;
          }
       }
   }


In the above code of .cs file 'b1' is showing 'null' value even after i click on the image.
Posted
Updated 30-Jul-13 2:04am
v2
Comments
Clifford Nelson 30-Jul-13 15:55pm    
I would stop the code in the debugger and look at the button and it's data context. Also, you should just cast, not use the sender as Button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900