473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 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_NewDesi gnerItem"
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

Oct 10 '08 #1
3 5293
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-*****@online.microsoft.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:
ms****@microsoft.com.

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

Oct 13 '08 #2
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-*****@online.microsoft.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:
ms****@microsoft.com.

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

Oct 13 '08 #3
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
Nov 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Graham | last post by:
I have created a custom MembershipProvider called "LassieMembershipProvider" that derives from "MembershipProvider". This providor is located in a Businesslogic layer dll called...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
27
by: Wayne | last post by:
I've been clicking around Access 2007 Beta 2 and can't see the custom menu bar designer. Is it in the beta? Maybe I'm blind. The question that comes to mind is: Will custom menu bars be the same...
2
by: Pon | last post by:
Hi everybody, I posted that on the Avalon ng but it seems there is not so much wpf developpers which use VB. Is there more VB developpers which work with WPF ? I'm trying to do a very simple...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
1
by: asharda | last post by:
I have a custom property grid. I am using custom property grid as I do not want the error messages that the propertygrid shows when abphabets are entered in interger fields. The custom property...
0
by: =?Utf-8?B?YV93YWhvbw==?= | last post by:
I am developing a diagramming project within C# using WPF. I based the visual representation off a tutorial from CodeProject <a...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.