Custom RoutedEvent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?YXdhaG9v?=

    Custom RoutedEvent

    Please forgive the re-post. I did not have a no-spam alias defined the first
    time I tried and that kept this message from appearing in the database.

    I am developing a diagramming project within C# using WPF. I based the
    visual representation off a tutorial from CodeProject
    (http://www.codeproject.com/KB/WPF/WP...er_Part4.aspx). I've
    added most of the extensions that I need for the solution but I'm new to WPF
    and having trouble getting RoutedEvents to work correctly.

    I have a canvas object which has several designerItems on it. The
    DesignerItem class has a RoutedEvent called NewDesignerItem Event which is
    fired whenever a new item is created:

    <code>
    /// <summary>
    /// Notify any listeners that a new Designer Item has been created.
    /// </summary>
    public static readonly RoutedEvent NewDesignerItem Event =
    EventManager.Re gisterRoutedEve nt("NewDesigner Item",
    RoutingStrategy .Bubble,
    typeof(RoutedEv entHandler),
    typeof(Designer Item));

    public event RoutedEventHand ler NewDesignerItem
    {
    add { AddHandler(NewD esignerItemEven t, value); }
    remove { RemoveHandler(N ewDesignerItemE vent, value); }
    }

    /// <summary>
    /// Fires the NewDesignerItem Event with a reference to this instance
    /// of the DesignerItem class
    /// </summary>
    private void NotifyNewDesign erItem()
    {
    RoutedEventArgs e = new
    RoutedEventArgs (DesignerItem.N ewDesignerItemE vent, this);
    RaiseEvent(e);
    }

    </code>

    Within my XAML I have added an EventHandler that should catch all
    NewDesignerItem events that are bubbling up:

    <code>
    <s:DesignerCanv as
    Focusable="true "
    x:Name="MyDesig ner"
    s:DesignerItem. NewDesignerItem ="MyDesigner_Ne wDesignerItem"
    Background="{St aticResource WindowBackgroun dBrush}"
    Margin="10" FocusVisualStyl e="{x:Null}"
    ContextMenu="{S taticResource DesignerCanvasC ontextMenu}"/>

    </code>

    The behavior I am expecting is that whenever the NewDesignerItem event is
    fired for a child within the DesignerCanvas object "MyDesigner " that event
    will be "bubled up" to my event handler. However, nothing happens! I can
    step through the creation of the DesignerItem and verify that my RoutedEvent
    is being called but the Handler never gets it.

    I was able to get the event handler to fire by using
    EventManager.Re gisterClassHand ler() within my Window constructor.
    Unfortunately, this implementation doesn't meet my goals because I will
    eventually have dozens of Canvas objects and I only want to handle the event
    on the canvas that "owns" the newly added DesignerItem.

    I did a sanity test by replacing my NewDesignerItem event with the
    ButtonBase.Clic k event and trapping that. This implementation works fine and
    my EventHandler is called as expected for all buttons contained in my
    DesignerCanvas.

    <code>
    <s:DesignerCanv as
    Focusable="true "
    x:Name="MyDesig ner"
    ButtonBase.Clic k="MyDesignerBu tton_Click"
    Background="{St aticResource WindowBackgroun dBrush}"
    Margin="10" FocusVisualStyl e="{x:Null}"
    ContextMenu="{S taticResource DesignerCanvasC ontextMenu}">
    </s:DesignerCanva s>
    </code>


    Thanks in advance!

    I'm using Visual Studio 2008 Professional with .NET Framework 3.5 SP1



  • Marco Zhou [MSFT]

    #2
    RE: Custom RoutedEvent

    Hello,

    Welcome to Microsoft Newsgroup Support Service! My name is Marco Zhou. It's
    my pleasure to work with you on this thread.

    If I understand you correctly, you want to fire your custom NewDesignerItem
    RoutedEvent when a specified DesignerItem object is added into
    DesignerCanvas design surface. If this is the case, you need to make sure
    that you fire the custom NewDesignerItem RoutedEvent after the specified
    DesignerItem object is added to the DesignerCanvas' s visual tree, one
    solution is to hook up to that DesignerItem object's Loaded event, and
    inside the Loaded event handler, you could call
    DesignerItem.No tifyNewDesigner Item() method to kick off the
    NewDesignerItem event routing starting from the newly added DesignerItem
    object to the DesignerCanvas object where you have NewDesignerItem event
    handler registered.

    If the solution I suggest above doesn't work for you, I would greatly
    appreciate it if you could create a small, complete and ready-to-run
    example to demonstrate the issue you are encountering, or you could
    directly send your project to me at v-mazho at Microsoft dot com for
    further research.

    If you have any further questions on this issue, free feel to ask here, we
    are glad to answer them.

    --------------------------------------------------
    Best regards,
    Macro Zhou (v-mazho@online.mi crosoft.com, remove 'online.')
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • =?Utf-8?B?YXdhaG9v?=

      #3
      RE: Custom RoutedEvent

      Thanks for the quick response. Your suggestion and explanation were perfect.
      I'm back on track and I truly appreciate the help.

      "Marco Zhou [MSFT]" wrote:
      Hello,
      >
      Welcome to Microsoft Newsgroup Support Service! My name is Marco Zhou. It's
      my pleasure to work with you on this thread.
      >
      If I understand you correctly, you want to fire your custom NewDesignerItem
      RoutedEvent when a specified DesignerItem object is added into
      DesignerCanvas design surface. If this is the case, you need to make sure
      that you fire the custom NewDesignerItem RoutedEvent after the specified
      DesignerItem object is added to the DesignerCanvas' s visual tree, one
      solution is to hook up to that DesignerItem object's Loaded event, and
      inside the Loaded event handler, you could call
      DesignerItem.No tifyNewDesigner Item() method to kick off the
      NewDesignerItem event routing starting from the newly added DesignerItem
      object to the DesignerCanvas object where you have NewDesignerItem event
      handler registered.
      >
      If the solution I suggest above doesn't work for you, I would greatly
      appreciate it if you could create a small, complete and ready-to-run
      example to demonstrate the issue you are encountering, or you could
      directly send your project to me at v-mazho at Microsoft dot com for
      further research.
      >
      If you have any further questions on this issue, free feel to ask here, we
      are glad to answer them.
      >
      --------------------------------------------------
      Best regards,
      Macro Zhou (v-mazho@online.mi crosoft.com, remove 'online.')
      Microsoft Online Community Support
      >
      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can improve the support we provide to you. Please
      feel free to let my manager know what you think of the level of service
      provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.
      >
      This posting is provided "AS IS" with no warranties, and confers no rights.
      >
      >

      Comment

      • Jennifer Overholt

        #4
        Same issue, just for image

        I have the same problem but cannot seem to get it working... except I need to do it on an image... I need to create a custom routed event that can get called to start my image eventtrigger, which ONLY takes RoutedEvent. So, need to create a custom one. All examples I've seen are not very helpful. The event I WANT to happen is off a DependencyPrope rtyChangedEvent Args but that won't work, so how do I make a custom routed event for an image to kick off a private void called DoSomething(){} ?

        Thanks in advance!
        Jennifer Overholt

        Comment

        Working...