开发者

How do I communicate between different Silverlight user controls and sub-pages?

开发者 https://www.devze.com 2023-02-11 13:45 出处:网络
I have 2 user controls on my Silverlight page.One contains a color picker (i.e. just some rectangles with colors in them and some code behind) and the other contains a listbox displaying items in the

I have 2 user controls on my Silverlight page. One contains a color picker (i.e. just some rectangles with colors in them and some code behind) and the other contains a listbox displaying items in the color selected in the color picker - or at least that's the idea. So far I have:

UI.MainPage mp = (UI.MainPage)App.Current.RootVisual;

from which I can get to the list box:

mp.ucPenStyles.lstPenStyles

but what to do next I don't know. I want the colors of the items in the listbox wo update when a new item is selected in the color picker. The lstbox is populated from 开发者_高级运维a collection via a method in the listbox user control code behind but I don't know how to access it.


Ok, I've thought a bit more about this. Both my user controls det properties in a separate static class. User contol A set a property in the clas which user control B must be able to detect has changed and update itself accordingly.


You're able to communicate between multiple Silverlight user controls on a single page using Local Connections quite easily. This requires Silverlight 3.0 or later.

http://geekswithblogs.net/PeterTweed/archive/2009/07/26/inter-silverlight-application-communication-in-silverlight-3.aspx has a good write-up on how to implement this feature.


If you are writing components using Prism for Silverlight, you can use the EventAggregator to send events between decoupled components.

Or, for a more simple application, you can expose a normal event from one of your controls and listen to it from the other control.


Not sure if this fits your requirements, but one or both controls can have a public static object that you can use to communicate via.

0

精彩评论

暂无评论...
验证码 换一张
取 消