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