views:

295

answers:

3

I have a navigation app with 5 pages. I also have a usercontrol with 3 radio buttons. This usercontrol is used by all 5 pages in my app. By default the first radio button is selected. However if the user clicks the 3rd radio button and I go to another page I want my usercontrol to still show that 3rd radio button as selected. How do I go about accomplishing this task?

A: 

There are several ways you could do this. Probably the most simple way is to create a class that has a property for the selected item of the radio buttons. Add an instance of this class as a resource to the application and then bind the radio buttons to this static resource.

public class MyState
{
  public string SelectedRadioValue {get;set;}
}

In the App.xaml.cs in the Application_Startup add:

var state = new MyState()
Resources.Add("myState", state);

Then in the bindings you can set:

SelectedValue="{Binding Source={StaticResource myState},Path=SelectedRadioValue}"

There are other approaches you could take as well.

Bryant
What I am trying to do is far more complex than a radio button(I just wanted to create a simple example of what I was after). I thought there would be an easier way to keep track of it than having to declare my own variables to do it.
Weston Goodwin
If you're trying to do something more complicated then you need to post a more complicated question. You can't expect people to answer a question that isn't asked.
Bryant
A: 

Is it possible to show this control outside of the View control? It sounds like it should be placed on the MainPage.xaml outside of the place where the views are displayed. This way the one control is used and its value can be made available to all of the views.

Jersey Dude
How do I do this? Do you have any sample code?
Weston Goodwin
A: 

If you are just looking to save the state of the page as the user navigates between pages then the easiest way of accomplishing this is to set:

NavigationCacheMode="Required"

in the first element of the xaml for your page.

Calanus