One option would be to bind the ContentControl
to a property in your model. And then update the Content
based on the value of another property.
Here is a very crude example using a CheckBox
, ContentControl
and a couple of UserControls
:
XAML
<StackPanel>
<CheckBox Content="Swap Content"
IsChecked="{Binding Path=Swapper, Mode=TwoWay}" />
<ContentControl Content="{Binding Path=ClockView}" />
</StackPanel>
Code Behind
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
this.DataContext = new MainModel();
}
}
public class MainModel : INotifyPropertyChanged
{
private bool _swapper;
public bool Swapper
{
get { return _swapper; }
set
{
_swapper = value;
NotifyChanged( "Swapper" );
SwapContent();
}
}
private UserControl _clockView;
public UserControl ClockView
{
get { return _clockView; }
private set
{
_clockView = value;
NotifyChanged( "ClockView" );
}
}
public void SwapContent()
{
// AnalogClock and DigitalClock are UserControls
if( ClockView == null || ClockView.GetType() == typeof( AnalogClock ) )
{
ClockView = new DigitalClock();
}
else
{
ClockView = new AnalogClock();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChanged( string propName )
{
if( PropertyChanged != null )
{
PropertyChanged( this, new PropertyChangedEventArgs( propName ) );
}
}
}