Image-Button Class Visibility WPF

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whunter31
    New Member
    • Apr 2010
    • 3

    #1

    Image-Button Class Visibility WPF

    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-> FrameworkElemen t -> Instance of the Button -> Field Data)? If not, what would the best way be to access the ImageButton instance and associated data? Can anyone please point me in the right direction?

    // WPF EventHandler at the container level:

    Code:
    <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; } 
            } 
        }
    Last edited by tlhintoq; Apr 6 '10, 05:16 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I am unable to access any of the class's field data.
      Unable to get the data, or you get the variable but it is blank?

      I suspect the problem is here
      Code:
      public SelectedPlanet(FrameworkElement fe) 
          { 
              return ((PlanetClass)(fe)); 
          }
      You do realize this is not searching for an existing planet object, right? It is trying to cast an 'fe' object to become a new 'PlanetClass' object.

      If the cast fails then you have no planet, and thus no PlanetName.
      If it succeeds, it still makes a NEW PlanetObject using the default constructor (no parameters) and again you have no PlanetName.

      Try this test. Change line 20 to give a default PlanetName and see if this is what you get.
      Code:
      string planetName = "Bob";
      Now when you properly make a planet you are going to rename it, so "Bob" is overwritten. But if you cast into a new planet without giving it a name you will see it is still Planet Bob.

      Comment

      • whunter31
        New Member
        • Apr 2010
        • 3

        #4
        tlhintoq -

        Thank you for your quick reply. Actually I was just beginning to understand how ((PlanetClass)( fe)) wasn't returning the instance of the class, but attempting to recast the frameworkElemen t "fe" into the PlanetClass class. What confused me was that if I added ".Content" to the end of ((PlanetClass)( fe)), I could see the instance and it's data; however I could not actually address the instance to access it's data, as in "Planet.Nam e".

        So, I guess I am back to square-one. Tlhintoq, would you possibly have any suggestion as to how to to access the data represented by an ImageButton, located in a ScrollViewer wrapped WrapPanel full of ImageButtons. Thanks once again.

        Bill

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I have no idea what a WrapPanel even is.
          I don't do WPF, if that helps.

          Comment

          • whunter31
            New Member
            • Apr 2010
            • 3

            #6
            tlhintoq -

            Thank you for your assistance. You've actually helped me realize where the problem lies.

            Regards,
            Bill

            Comment

            Working...