WPF - How does Parent know of on “Checked” event for child control checkbox?

Up vote 1 down vote favorite share g+ share fb share tw.

I have a child control that has a checkbox and webbrowser: I am putting this control in my MainWindow. Xaml/. Cs: My question is how can my MainWindow know that the CheckBox (chkA) has been checked?

So far only the actual user control knows it has been clicked? How can I expose the "Checked" event for my MainWindow to see? Or is there a better way?

I've searched the web, but can't seem to wrap my head around what I'm seeing. Thank you in advance, -newb EDIT 1: I am trying the following with no luck, but may be on the right track. Within my MainWindow.xaml.

Cs I have added: public static readonly RoutedEvent CheckedEvent = EventManager. RegisterRoutedEvent("Checked", RoutingStrategy. Bubble, typeof(RoutedEventHandler), typeof(MyUserControl)); public event RoutedEventHandler Checked { add { AddHandler(CheckedEvent, value); } remove { RemoveHandler(CheckedEvent, value); } } And within MyUserControl.xaml.

Cs I have added: private void chkA_Checked(object sender, RoutedEventArgs e) { RoutedEventArgs args = new RoutedEventArgs(MainWindow. CheckedEvent); RaiseEvent(args); } EDIT 2: Moved previously mentioned code into MyUserControl.xaml. Cs: public static readonly RoutedEvent CheckedEvent = EventManager.

RegisterRoutedEvent("Checked", RoutingStrategy. Bubble, typeof(RoutedEventHandler), typeof(MyUserControl)); public event RoutedEventHandler Checked { add { AddHandler(CheckedEvent, value); } remove { RemoveHandler(CheckedEvent, value); } } private void chkA_Checked(object sender, RoutedEventArgs e) { RoutedEventArgs args = new RoutedEventArgs(MainWindow. CheckedEvent); RaiseEvent(args); } And now I am able to see the "Checked" event "Bubble" up as so: Thanks @Matt for the tip!

EDIT 3: Matt's first answer is the best way to go, I had it "Checkbox" vs "CheckBox"... Adding CheckBox. Checked to the grid catches the event: c# wpf checkbox wpf-controls wpf-usercontrols link|improve this question edited Apr 27 '11 at 22:33 asked Apr 27 '11 at 21:14Mike174 60% accept rate.

Let the Checked event bubble up to the parent and catch it there. It's the beauty of routed events!

I haven't had a chance to test this, but I'm 99% certain it'll work even if the CheckBoxes are inside a UserControl. Certainly it works if they're in a DataTemplate. – Matt Hamilton Apr 27 '11 at 21:20 @Matt-Hamilton Thanks for the response, I've editted my questions with additional info as I look more into routed events.

– Mike Apr 27 '11 at 22:07 @Mike - you don't need any extra code. All you need, as per my answer, is to handle CheckBox. Checked in the "host" Window somewhere and any Checked event in your UserControl will bubble up to there.

– Matt Hamilton Apr 27 '11 at 22:25 @Matt-Hamilton You were totally right the first time, my typo caused my a "The attached property 'Checked' was not found in type 'Checkbox'. " So I went looking up routed events. At least now I know more about routed events.

Thanks for the help, it is much appreciated! – Mike Apr 27 '11 at 22:35.

You're on the right track, but you should declare the routed event in MyUserControl, not in MainWindow... it's the user control that is raising the event, not the window. The way you're doing it, your control is explicitly depending on MainWindow, which is bad practice and not necessary.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions