Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / WPF
Alternative
Tip/Trick

Passing events from child to parent control's built in handler in WPF

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Jul 2011CPOL1 min read 21.6K   1   5
In WPF, you can use the method UIElement.AddHandler(RoutedEvent, Delegate, Boolean) to specify that the UIElement should receive all events, including those that have been handled. Just subscribe your ListBox to the MouseDown event this way (you might do this in your custom control's...

In WPF, you can use the method UIElement.AddHandler(RoutedEvent, Delegate, Boolean) to specify that the UIElement should receive all events, including those that have been handled. Just subscribe your ListBox to the MouseDown event this way (you might do this in your custom control's constructor):


C#
listBox.AddHandler(Mouse.MouseDownEvent, ListBox_MouseDown, true);

Then in ListBox_MouseDown, you'll need to extract the item that has been clicked. It can be done by iterating through ListBox items and searching for the one that is the parent for the RoutedEventArgs.OriginalSource of the event.


But it seems to me that what you really need is setting the ListBox.SelectedItem to the TextBox that has focus (what if the user tabs through your TextBoxes?). This can be achieved by handling the GotFocus event on the ListBoxItems:


XML
<ListBox>
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <EventSetter Event="GotFocus" Handler="ListBoxItem_GotFocus"/>
    </Style>
  </ListBox.ItemContainerStyle>
		
  <TextBox/>
  <TextBox/>
</ListBox>

The code:


C#
private void ListBoxItem_GotFocus(object sender, RoutedEventArgs e)
{
  var listBoxItem = sender as ListBoxItem;
  listBoxItem.IsSelected = true;
}

This way is perhaps closer to your real UI logic, and it gives higher performance. This is because in WPF, setting RoutedEvent.Handled to true does not stop the event from bubbling or tunneling if it does. They continue propagating through the element tree, but only handlers registered as shown above are invoked on the way. Thus, you have doubled the number of MouseDown events raised in the system, which is obviously slower than following my advice.


Best regards,

Pavel.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
The MS Team, LLC.
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI've revoted. I'm not sure if it's possible, but it seems t... Pin
GATzilla3-Aug-11 1:07
GATzilla3-Aug-11 1:07 
GeneralRe: Good idea. I've updated my tip to set HandleEventsToo in XAM... Pin
XiaoChuan Yu4-Aug-11 6:43
XiaoChuan Yu4-Aug-11 6:43 
GeneralThanks, you made some very good points especially about usin... Pin
XiaoChuan Yu30-Jul-11 7:20
XiaoChuan Yu30-Jul-11 7:20 
GeneralNow I see your actual problem. Seems like delivering the ori... Pin
GATzilla28-Jul-11 17:56
GATzilla28-Jul-11 17:56 
GeneralHi, thanks for the comment. Sorry I didn't make it clear tha... Pin
XiaoChuan Yu27-Jul-11 12:49
XiaoChuan Yu27-Jul-11 12:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.