locked
Event listening of a code generated custom control (Chiya) - 1/25/2007 10:20 AM PST RRS feed

  • Question

  • By: Chiya

    Hello,
    
    I want to ask a question about event listening.
    
    The scenario is the following:
    - Item (one of the items contained in a ListBox, generetad via DataBinding)
    - Detail (custom control contained in Items Template, it has a variable 
    saving the Small object generated by it)
    - Small (custom object generated by a Detail object onMouseEnter, it is 
    located riht over the Detail generator)
    
    Essentially the Small object should collapse onMouseLeave.
    The problem is: if I associate a handler to onMouseLeave event directly in 
    the Small.xaml and in the Small.xaml.cs file, when that event is raised the 
    application crashes.
    
    (code:
    Small s = sender as Small;
    s.Visibility = Visibility.Collapsed;)
    
    Should I listen to that event from Detail object (which, as said before, 
    contains a variable pointing to the generated Small object)? If so, do I have 
    to take care of it by code? Is there no way to take care of it by IDE? And 
    could you possibly give me a hint on how to do that?
    
    Thanks
    Serena
    Tuesday, February 19, 2008 7:48 PM

Answers

  • By: Lingesh Palaniappan (MS)

    Hi,
    The code that you have should work. Could you tell us what the exception is?
    Is it possible that when you cast your sender as Small you get a null,
    meaning the object that is sending you the mouseleave event is not small at
    all.
    I could think of a couple of things assuming the above hypothesis is
    correct:
    1) You have attached the event handler to some element with in small's
    template so the sender is that element and not small. In that case you can
    get small using the template parent property.
    2) You have 2 mouseleave event handlers.

    You can find the type of sender in Visual studio by putting a break point in
    that line and then finding its type.
    Thanks!
    regards,
    Lingesh

    --
    This posting is provided "AS IS" with no warranties, and confers no rights.
    "Chiya" <Chiya@discussions.microsoft.com> wrote in message
    news:B65D9A58-A13C-425D-A2CE-343E70F4A9E0@microsoft.com...
    > Hello,
    >
    > I want to ask a question about event listening.
    >
    > The scenario is the following:
    > - Item (one of the items contained in a ListBox, generetad via
    > DataBinding)
    > - Detail (custom control contained in Items Template, it has a variable
    > saving the Small object generated by it)
    > - Small (custom object generated by a Detail object onMouseEnter, it is
    > located riht over the Detail generator)
    >
    > Essentially the Small object should collapse onMouseLeave.
    > The problem is: if I associate a handler to onMouseLeave event directly in
    > the Small.xaml and in the Small.xaml.cs file, when that event is raised
    > the
    > application crashes.
    >
    > (code:
    > Small s = sender as Small;
    > s.Visibility = Visibility.Collapsed;)
    >
    > Should I listen to that event from Detail object (which, as said before,
    > contains a variable pointing to the generated Small object)? If so, do I
    > have
    > to take care of it by code? Is there no way to take care of it by IDE? And
    > could you possibly give me a hint on how to do that?
    >
    > Thanks
    > Serena

    Tuesday, February 19, 2008 10:47 PM

All replies

  • By: Lingesh Palaniappan (MS)

    Hi,
    The code that you have should work. Could you tell us what the exception is?
    Is it possible that when you cast your sender as Small you get a null,
    meaning the object that is sending you the mouseleave event is not small at
    all.
    I could think of a couple of things assuming the above hypothesis is
    correct:
    1) You have attached the event handler to some element with in small's
    template so the sender is that element and not small. In that case you can
    get small using the template parent property.
    2) You have 2 mouseleave event handlers.

    You can find the type of sender in Visual studio by putting a break point in
    that line and then finding its type.
    Thanks!
    regards,
    Lingesh

    --
    This posting is provided "AS IS" with no warranties, and confers no rights.
    "Chiya" <Chiya@discussions.microsoft.com> wrote in message
    news:B65D9A58-A13C-425D-A2CE-343E70F4A9E0@microsoft.com...
    > Hello,
    >
    > I want to ask a question about event listening.
    >
    > The scenario is the following:
    > - Item (one of the items contained in a ListBox, generetad via
    > DataBinding)
    > - Detail (custom control contained in Items Template, it has a variable
    > saving the Small object generated by it)
    > - Small (custom object generated by a Detail object onMouseEnter, it is
    > located riht over the Detail generator)
    >
    > Essentially the Small object should collapse onMouseLeave.
    > The problem is: if I associate a handler to onMouseLeave event directly in
    > the Small.xaml and in the Small.xaml.cs file, when that event is raised
    > the
    > application crashes.
    >
    > (code:
    > Small s = sender as Small;
    > s.Visibility = Visibility.Collapsed;)
    >
    > Should I listen to that event from Detail object (which, as said before,
    > contains a variable pointing to the generated Small object)? If so, do I
    > have
    > to take care of it by code? Is there no way to take care of it by IDE? And
    > could you possibly give me a hint on how to do that?
    >
    > Thanks
    > Serena

    Tuesday, February 19, 2008 10:47 PM
  • By: Chiya

    Hi Lingesh,
    
    you got the mistake! (stupidme)
    I just had added 2 event listeners both to Small and to a Border inside 
    Small.xaml. I think it crushed because of the second, trying to transform 
    Border in Small (since the method was unique).
    
    Thanks a lot
    
    Serena
    
    "Lingesh Palaniappan (MS)" wrote:
    
    > Hi,
    > The code that you have should work. Could you tell us what the exception is? 
    > Is it possible that when you cast your sender as Small you get a null, 
    > meaning the object that is sending you the mouseleave event is not small at 
    > all.
    > I could think of a couple of things assuming the above hypothesis is 
    > correct:
    > 1) You have attached the event handler to some element with in small's 
    > template so the sender is that element and not small. In that case you can 
    > get small using the template parent property.
    > 2) You have 2 mouseleave event handlers.
    > 
    > You can find the type of sender in Visual studio by putting a break point in 
    > that line and then finding its type.
    > Thanks!
    > regards,
    > Lingesh
    > 
    > -- 
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    > "Chiya" <Chiya@discussions.microsoft.com> wrote in message 
    > news:B65D9A58-A13C-425D-A2CE-343E70F4A9E0@microsoft.com...
    > > Hello,
    > >
    > > I want to ask a question about event listening.
    > >
    > > The scenario is the following:
    > > - Item (one of the items contained in a ListBox, generetad via 
    > > DataBinding)
    > > - Detail (custom control contained in Items Template, it has a variable
    > > saving the Small object generated by it)
    > > - Small (custom object generated by a Detail object onMouseEnter, it is
    > > located riht over the Detail generator)
    > >
    > > Essentially the Small object should collapse onMouseLeave.
    > > The problem is: if I associate a handler to onMouseLeave event directly in
    > > the Small.xaml and in the Small.xaml.cs file, when that event is raised 
    > > the
    > > application crashes.
    > >
    > > (code:
    > > Small s = sender as Small;
    > > s.Visibility = Visibility.Collapsed;)
    > >
    > > Should I listen to that event from Detail object (which, as said before,
    > > contains a variable pointing to the generated Small object)? If so, do I 
    > > have
    > > to take care of it by code? Is there no way to take care of it by IDE? And
    > > could you possibly give me a hint on how to do that?
    > >
    > > Thanks
    > > Serena 
    > 
    > 
    > 
    Tuesday, February 19, 2008 10:47 PM