views:

208

answers:

4

I am working in SL version 2.0

I have 3 radio buttons in a group and 3 user controls that I want to load based on the selection of the radio button. How can this be done? What options do I have?

I was thinking about adding the user controls in the same xaml file as the radio buttons, and make them all invisible. Then on the code behind depending on the selection of the radio buttons make them visible.

Is that the only solution or is there a more effective way to do this?

Thanks in advance.

A: 

Depending on the size and complexity of those controls, your solution may be reasonable.

There are other methods, including dynamically loading modules. This adds significant complexity. Prism is the foremost project for this in Silverlight, and can be found at http://compositewpf.codeplex.com/.

10 things to know about Prism : http://www.sparklingclient.com/prism-silverlight/
Podcast on intro to Prism : http://www.sparklingclient.com/prism-in-silverlight/

Jay
Although I have heard about Prism before, I am not at all familiar with it. Thanks for providing the links :-)
VoodooChild
A: 

An easy way to do this would be to use a TabControl, put your UserControls in TabItems and re-style the TabItem.Header to show a RadioButton instead. This will take care of the showing and hiding without mucking up your code with manually setting Visibility properties everywhere

qntmfred
good idea, but these controls are already in a tabitem and are not very big, so having another tab-control for it won't look pretty.~thanks~
VoodooChild
remember, a TabControl doesn't have to look like a TabControl. you just want its behavior. You can easily restyle the TabItem.Header and nobody would be able to tell the difference.
qntmfred
A: 

A simple way that I can think of, is to have a placeholder where you want the control to be loaded (e.g. StackPanel) and whenever a radio button is selected, clear the StackPanel.Children and add a new instance of the new UserControl.

Johannes
A: 

Another option would be to load the XAML for the controls dynamically using XamlReader.Load() - you can cast the resulting object to a FrameworkElement and add it to the control tree.

XamlReader.Load details: http://msdn.microsoft.com/en-us/library/cc190359%28VS.95%29.aspx

E.Z. Hart