2012-07-20 85 views
0

我有一個名爲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

回答

3

我想你可以只訂閱事件......這些方針的東西:

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

希望它可以幫助你找到一個方法來待着......

0

彼此接近這些控件其他?那麼在內部烘焙一個新的用戶控件也許會有意義。該外部用戶控件是應該執行事件處理的控件。

相關問題