I'm 开发者_运维知识库pretty new to silverlight so, i'm having this problem about communication between user controls. I have user controls that have buttons in them which are supposed to set some properties of other user controls. For example, IsEnabled property to be set as true or false or visibility, and so on. I actually know one solution which would be something like:
class UserControl1 : Usercontrol
{ public UserControl2 uc2;private void Button1_Click(object sender, RoutedEventArgs e)
{ uc2.IsEnabled=False; // or uc2.SomeMethod(); } }Similar goes for UserControl2 class, and then in main page i only add:
UserControl1.uc2=UserControl2;My questions is, how can i do this via Event Handlers? Or maybe there is some othe better solution? A simple example would be welcome. Thanks.
Another approach is to use a Event Aggregator for such communication. We in our project are using Prism's event aggregator. Please check the following thread.
Things to keep in mind while using event aggregator are
- Keep their usage to minimal. This is because event subscriptions using event aggregator may be difficult to debug. So within the same class use normal events.
- Name the events when using event aggregator in a way that describes the event. For example if you click a save button to save customer, use a event name such as BeforeCustomerSave\CustomerSaved instead of SaveButtonClicked.
精彩评论