Click here to Skip to main content
12,404,478 members (69,967 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.7K views
1 bookmarked
Posted

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

, 26 Jul 2011 CPOL
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

You may also be interested in...

Comments and Discussions

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by GATzilla
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid