I have a child control that has a checkbox and webbrowser:
<UserControl x:Class="Some.MyUserControl" etc.>
<Grid>
<CheckBox x:Name="chkA" Content="Analysis" Checked="chkA_Checked"></CheckBox>
<WebBrowser Margin="0,30,0,0" Name="wbA"></WebBrowser>
</Grid>
</UserControl>
I am putting this control in my MainWindow.xaml/.cs:
<Window x:Class Some.MainWindow xmlns:local="clr-namespace:Some" etc.>
<Grid>
<local:MyUserControl x:Name="MyUserControl_Main"></local:MyUserControl>
</Grid>
</Window>
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:
<Window x:Class Some.MainWindow xmlns:local="clr-namespace:Some" etc.>
<Grid>
<local:MyUserControl x:Name="MyUserControl_Main" **Checked="MyUserControl_Main_Checked"**></local:MyUserControl>
</Grid>
</Window>
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:
Let the
Checkedevent bubble up to the parent and catch it there. It’s the beauty of routed events!