简体   繁体   English

WPF - 家长如何知道子控件复选框的“已选中”事件?

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

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:我将此控件放在我的 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?我的问题是我的 MainWindow 怎么知道 CheckBox (chkA) 已被选中? So far only the actual user control knows it has been clicked?到目前为止只有实际的用户控件知道它被点击了吗? How can I expose the "Checked" event for my MainWindow to see?如何公开“已检查”事件以供 MainWindow 看到? Or is there a better way?或者,还有更好的方法?

I've searched the web, but can't seem to wrap my head around what I'm seeing.我搜索了 web,但似乎无法理解我所看到的。

Thank you in advance,先感谢您,

-newb -newb

EDIT 1:编辑1:

I am trying the following with no luck, but may be on the right track.我正在尝试以下没有运气,但可能在正确的轨道上。

Within my MainWindow.xaml.cs I have added:在我的 MainWindow.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); }
}

And within MyUserControl.xaml.cs I have added:在 MyUserControl.xaml.cs 我添加了:

private void chkA_Checked(object sender, RoutedEventArgs e)
{
    RoutedEventArgs args = new RoutedEventArgs(MainWindow.CheckedEvent);
    RaiseEvent(args);
}

EDIT 2:编辑2:

Moved previously mentioned code into MyUserControl.xaml.cs:将前面提到的代码移到 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!感谢@Matt 的提示!

EDIT 3:编辑 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:马特的第一个答案是 go 的最佳方式,我有它“复选框”与“复选框”......将 CheckBox.Checked 添加到网格会捕获事件:

Let the Checked event bubble up to the parent and catch it there.Checked事件冒泡到父级并在那里捕获它。 It's the beauty of routed events!这是路由事件的美妙之处!

<Grid CheckBox.Checked="CheckBox_Checked">
    <local:MyUserControl x:Name="MyUserControl_Main" />
</Grid>

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.您在正确的轨道上,但您应该在MyUserControl中声明路由事件,而不是在MainWindow中......引发事件的是用户控件,而不是 window。 The way you're doing it, your control is explicitly depending on MainWindow , which is bad practice and not necessary.你这样做的方式,你的控制是明确地依赖于MainWindow ,这是不好的做法,没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM