0

I have a Window called MainWindow which contains 2 UserControls:

SearchBox

and

CustomerList

When someone types into SearchBox I want to change CustomerList accordingly.

Question: What is the best way for CustomerList to know that the TextChanged event has been executed in SearchBox?

Right now I have SearchBox defined as:

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));
    }
}

and MainWindow has the following XAML and EventHandler:

<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
}

Do I need to pass the event from SearchSubmitted_Invoked to CustomerList? Is there a way for me to pass it directly from SearchBox to CustomerList?

4

2 に答える 2

3

私はあなたがただイベントを購読することができると思います...これらの線に沿った何か:

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

それがあなたが一緒に動く方法を見つけるのを助けることを願っています...

于 2012-07-20T10:17:41.027 に答える
0

それらのコントロールは互いに近くにありますか? 次に、それらを内部に含む新しいユーザー コントロールを作成するのが理にかなっているかもしれません。その外側のユーザー コントロールは、イベント処理を行う必要があります。

于 2012-07-20T13:25:56.903 に答える