如何在父控件中处理子事件

发布于 2024-12-17 15:13:22 字数 103 浏览 2 评论 0原文

在我的主窗口中,我有一个子控件(用户控件),其中包含一个文本框。如何处理主(父)窗口中子控件文本框的textchange事件。

请为我提供一些代码示例,因为我是事件路由的新手。

In my main window, I have a child control(user control) which contains a text box . How can I handle the textchange event of the text box of child control in main(parent) window.

Please provide me some example with code as I am new to routing of events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

刘备忘录 2024-12-24 15:13:22

您应该能够从父控件中挂钩该事件。但由于您的父控件没有自己的 TextChanged 事件,因此您需要使用附加属性语法:

<Window ...
        TextBox.TextChanged="ChildTextBoxChanged">

并且在您的代码隐藏中:

private void ChildTextBoxChanged(object sender, TextChangedEventArgs args)
{
    ...
}

您不必将 具体来说,Window 上的 TextBox.TextChanged= - 只是 TextBox 父级的任何控件,即 父级的任何控件用户控件。该事件将依次冒泡到每个父级,一直到顶级 Window,并且可以在整个过程中的任何位置进行处理。

(请注意,如果有人挂钩该事件并设置e.Handled = true,该事件将不会冒泡超过该点。这对于了解您是否有多个级别的处理程序很有用。)

You should just be able to hook the event from the parent control. But since your parent control doesn't have a TextChanged event of its own, you'll need to use attached-property syntax:

<Window ...
        TextBox.TextChanged="ChildTextBoxChanged">

and in your codebehind:

private void ChildTextBoxChanged(object sender, TextChangedEventArgs args)
{
    ...
}

You don't have to put the TextBox.TextChanged= on the Window specifically -- just any control that's a parent of the TextBox, i.e., any control that's a parent of your UserControl. The event will bubble up to each parent in turn, all the way up to the top-level Window, and can get handled anywhere along the way.

(Note that if anyone hooks the event and sets e.Handled = true, the event won't bubble past that point. Useful to know if you have handlers at multiple levels.)

故事未完 2024-12-24 15:13:22

这也对我有帮助。我将在子控件的容器中创建一个事件,并在代码隐藏文件中定义该事件。该事件将为所有子级处理所有文本更改事件。

       <StackPanel TextBoxBase.TextChanged="test_TextChanged" Name="test">                                    
                <userControl/>
       </StackPanel>

this also helped me. I will have a event in the container of the child control and define the event in the code behind file. The event will handle all the text changed events for all the children.

       <StackPanel TextBoxBase.TextChanged="test_TextChanged" Name="test">                                    
                <userControl/>
       </StackPanel>
葮薆情 2024-12-24 15:13:22

在子控件中创建一个事件 -

public event TextChangedEventHandler TextChanged;

现在为子控件中的 TextBox 的 TextChanged 事件添加一个处理程序 -

private void TextBox_TextChanged(object sender, TextChangedEventArgs args)
{
    if (TextChanged != null)
    {
        TextChanged.Invoke(this, args);
    }
}

还为此处理程序更新 XAML -

<TextBox ... TextChanged="TextBox_TextChanged" ... />

现在,您已经在子控件中创建了一个事件,该事件在 Textbox 的 textchanged 触发时触发。

现在您只需在主窗口中添加此事件的处理程序 -

private void ChildControl_TextChanged(object sender, TextChangedEventArgs args)
{
   //TODO: Add your further code here.
}

Create a event in your childcontrol -

public event TextChangedEventHandler TextChanged;

now add a handler for TextChanged event of TextBox in childcontrol -

private void TextBox_TextChanged(object sender, TextChangedEventArgs args)
{
    if (TextChanged != null)
    {
        TextChanged.Invoke(this, args);
    }
}

also update XAML for this handler -

<TextBox ... TextChanged="TextBox_TextChanged" ... />

Now, you have created a event in your childcontrol that fires when the Textbox's textchanged fires.

Now you only to add a handler for this event in mainwindow -

private void ChildControl_TextChanged(object sender, TextChangedEventArgs args)
{
   //TODO: Add your further code here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文