我有一個名爲MainWindow
窗口其中包含2個用戶控件:如何通知UserControl在另一個UserControl中發生了事件?
SearchBox
和
CustomerList
當有人類型分爲搜索盒我要隨之改變CustomerList。
問題:什麼是CustomerList
知道,TextChanged
事件已SearchBox
被執行的最佳方法?
現在我有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));
}
}
和MainWindow
具有以下XAML和事件處理程序:
<UserControls:SearchBox SearchSubmitted="SearchSubmitted_Invoked" />
private void SearchSubmitted_Invoked(object sender, SearchSubmittedEventArgs e)
{
// I know SearchBox has been typed into
// but I don't know how best to inform CustomerList
}
我需要從SearchSubmitted_Invoked
事件傳遞給CustomerList
?有沒有辦法讓我直接從SearchBox
傳遞給CustomerList
?