Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

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

, 26 Jul 2011
Rate this:
Please Sign up or sign in to vote.
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):

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:

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

The code:

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)

Share

About the Author

GATzilla
The MS Team, LLC.
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by GATzilla
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid