Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通知UserControl另一个UserControl中发生了事件?_C#_Wpf_Events_Xaml_Event Handling - Fatal编程技术网

C# 如何通知UserControl另一个UserControl中发生了事件?

C# 如何通知UserControl另一个UserControl中发生了事件?,c#,wpf,events,xaml,event-handling,C#,Wpf,Events,Xaml,Event Handling,我有一个名为MainWindow的窗口,其中包含2个用户控件: SearchBox 及 当有人在SearchBox中键入内容时,我想相应地更改CustomerList 问题:要让客户列表知道TextChanged事件已在搜索框中执行,最好的方法是什么 现在我将SearchBox定义为: public class SearchSubmittedEventArgs : EventArgs { public string Term { get; set; } public Sear

我有一个名为
MainWindow
的窗口,其中包含2个用户控件:

SearchBox

当有人在SearchBox中键入内容时,我想相应地更改CustomerList

问题:要让
客户列表
知道
TextChanged
事件已在
搜索框中执行,最好的方法是什么

现在我将
SearchBox
定义为:

public class SearchSubmittedEventArgs : EventArgs
{
    public string Term { get; set; }

    public SearchSubmittedEventArgs(string term)
    {
        Term = term;
    }
}

public partial class SearchBox : UserControl
{
    public event EventHandler<SearchSubmittedEventArgs> SearchSubmitted;
    public SearchBox()
    {
        InitializeComponent();
    }

    private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (SearchSubmitted != null) SearchSubmitted.Invoke(this, new SearchSubmittedEventArgs(((TextBox)sender).Text));
    }
}

我是否需要将事件从
SearchSubmitted\u Invoked
传递到
CustomerList
?有没有办法让我直接将它从
搜索框
传递到
客户列表

我想您可以订阅活动……大致如下:

 var search = new SearchBox();
 var cust = new CustomerList();
 search.SearchSubmitted += (s,e)=>{ cust.Update(e.Term); }

希望它能帮助您找到一种前进的方式……

我想您可以订阅该活动……大致如下:

 var search = new SearchBox();
 var cust = new CustomerList();
 search.SearchSubmitted += (s,e)=>{ cust.Update(e.Term); }

希望它能帮助您找到一种前进的方式…

这些控件彼此靠近吗?然后,在它们内部烘焙一个新的用户控件可能是有意义的。外部用户控件是应该进行事件处理的控件。

这些控件彼此靠近吗?然后,在它们内部烘焙一个新的用户控件可能是有意义的。外部用户控件是应该进行事件处理的控件

 var search = new SearchBox();
 var cust = new CustomerList();
 search.SearchSubmitted += (s,e)=>{ cust.Update(e.Term); }