Custom RoutedEvent

A

awahoo

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/WPFDiagramDesigner_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 NewDesignerItemEvent 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 NewDesignerItemEvent =
EventManager.RegisterRoutedEvent("NewDesignerItem",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(DesignerItem));

public event RoutedEventHandler NewDesignerItem
{
add { AddHandler(NewDesignerItemEvent, value); }
remove { RemoveHandler(NewDesignerItemEvent, value); }
}

/// <summary>
/// Fires the NewDesignerItemEvent with a reference to this instance
/// of the DesignerItem class
/// </summary>
private void NotifyNewDesignerItem()
{
RoutedEventArgs e = new
RoutedEventArgs(DesignerItem.NewDesignerItemEvent, this);
RaiseEvent(e);
}

</code>

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

<code>
<s:DesignerCanvas
Focusable="true"
x:Name="MyDesigner"
s:DesignerItem.NewDesignerItem="MyDesigner_NewDesignerItem"
Background="{StaticResource WindowBackgroundBrush}"
Margin="10" FocusVisualStyle="{x:Null}"
ContextMenu="{StaticResource DesignerCanvasContextMenu}"/>

</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.RegisterClassHandler() 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.Click event and trapping that. This implementation works fine and
my EventHandler is called as expected for all buttons contained in my
DesignerCanvas.

<code>
<s:DesignerCanvas
Focusable="true"
x:Name="MyDesigner"
ButtonBase.Click="MyDesignerButton_Click"
Background="{StaticResource WindowBackgroundBrush}"
Margin="10" FocusVisualStyle="{x:Null}"
ContextMenu="{StaticResource DesignerCanvasContextMenu}">
</s:DesignerCanvas>
</code>


Thanks in advance!

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

Marco Zhou [MSFT]

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.NotifyNewDesignerItem() 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 ([email protected], 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:
(e-mail address removed).

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

awahoo

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

Jennifer Overholt

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 DependencyPropertyChangedEventArgs 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top