views:

22

answers:

1

I am attempting to obtain class-data associated with a mouse-clicked ImageButton; which ImageButton is located within a Scollviewer wrapped WrapPanel and filled with numerous other ImageButtons. The problem is that although I can see the instance of the ImageButton selected "((PlanetClass)(fe))", and have visibility of the class instance's underlying data "((PlanetClass)(fe)).Content", I am unable to access any of the class's field data. The example below illustrates my intention.

Am I approaching this problem correctly (WrapPanel (wrapped in ScrollViewer)-> ImageButton-> FrameworkElement -> Instance of the Button -> Field Data)? If not, what would the best way be to access the ImageButton instance and the instance's associated data? Can anyone please point me in the right direction?

// WPF EventHandler at the container level:
<ScrollViewer ButtonBase.Click="SolarSystem_Click">

// Handles the ImageButton mouseClick event within the ScrollViewer wrapping the WrapPanel. 
private void SolarSystem_Click(Object sender, RoutedEventArgs e) 
{ 
    FrameworkElement fe = e.OriginalSource as FrameworkElement; 
    SelectedPlanet PlanetSelected = new SelectedPlanet(fe); 
    MessageBox.Show(PlanetSelected.PlanetName); 
} 

// Used to initiate instance of ImageButton to access field data. 
public SelectedPlanet(FrameworkElement fe) 
{ 
    return ((PlanetClass)(fe)); 
} 

// Class Data 
public class PlanetClass 
{ 
    string planetName; 

    public PlanetClass(string planetName) 
    { 
        PlanetName = planetName; 
    } 

    public string PlanetName 
    { 
        set { planetName = value; } 
        get { return planetName; } 
    } 
} 
A: 

TWO DAYS LATER and a lot of frustration:

After scratching my head for two days on this issue, I figured out that to obtain the underlying data beneath a mouse-clicked ImageButton I needed to cast the FrameworkElement e.OriginalSource back to the original ImageButton to get to it's ".source", and then cast the result to PlanetClass to get to it's properties.

// WPF EventHandler placed at the container level.
<ScrollViewer ButtonBase.Click="SolarSystemButton_Click">

// Handles the ImageButton mouseClick event within the ScrollViewer wrapping the WrapPanel. 
 private void SolarSystemButton_Click(Object sender, RoutedEventArgs e)  
     { 
         FrameworkElement fe = e.OriginalSource as FrameworkElement;  
         string PlanetName = ((PlanetClass)((ImageButton)fe).Content).PlanetName;
         return PlanetName;
     }

Bill

Bill