PC Review


Reply
Thread Tools Rate Thread

Custom RoutedEvent

 
 
awahoo
Guest
Posts: n/a
 
      10th Oct 2008
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...ner_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>
<sesignerCanvas
Focusable="true"
x:Name="MyDesigner"
sesignerItem.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>
<sesignerCanvas
Focusable="true"
x:Name="MyDesigner"
ButtonBase.Click="MyDesignerButton_Click"
Background="{StaticResource WindowBackgroundBrush}"
Margin="10" FocusVisualStyle="{x:Null}"
ContextMenu="{StaticResource DesignerCanvasContextMenu}">
</sesignerCanvas>
</code>


Thanks in advance!

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



 
Reply With Quote
 
 
 
 
Marco Zhou [MSFT]
Guest
Posts: n/a
 
      13th Oct 2008
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 (v-(E-Mail Removed), 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 Removed).

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

 
Reply With Quote
 
awahoo
Guest
Posts: n/a
 
      13th Oct 2008
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.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 (v-(E-Mail Removed), 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 Removed).
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Jennifer Overholt
Guest
Posts: n/a
 
      6th Nov 2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RoutedEvent from a class not firing a_wahoo Microsoft C# .NET 0 1st Oct 2008 10:38 PM
How to create WPF Custom routedevent in VB ? Pon Microsoft VB .NET 2 6th Feb 2007 05:06 PM
How to create custom profiles for windows Movie Maker(with custom bitrate, FPS, resolution) mistral Windows XP MovieMaker 2 5th Jan 2007 05:50 PM
How to create WPF Custom routedevent in VB ? Pon Microsoft VB .NET 0 16th Nov 2006 09:31 AM
Forward item custom form and pooling custom transport provider Tomislav Petrovic Microsoft Outlook Form Programming 3 25th Oct 2004 05:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:38 AM.