I have a content control that shows dynamic content based on the current state. This all works fine but, for design time, i'd like it to show the default state. Is there any way I can do this using either the ValueConverter
or FallbackValue
or something?
XAML
<ContentControl Content="{Binding State,
Converter={StaticResource InstallationStateToControlConverter}}" />
C#
class InstallationStateToControlConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//return controls depending on the state
switch ((InstallationState)value)
{
case InstallationState.LicenceAgreement:
return new LicenceAgreementControl();
default:
return new AnotherControl();
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Update
As per Viv's question I have added the following to my XAML, it compiles ok but I still see nothing in the designer?
d:DataContext="{d:DesignInstance Type=local:LicenceAgreementControl, IsDesignTimeCreatable=True}"
Ok I got it to work finally,
It was a combination of multiple things from the comments. Hope this solves it for you.
Assuming everything run-time is fine wrt to view models showing in ContentControl
and sorts
These were the steps I did.
ContentControl
Content
Binding Value ( State
in your case) the default ViewModel to be shown. Example:
public LicenceAgreementControl() {
State = new NewViewModel();
}
d:DataContext
from your main xaml file. DataContext
to your ContentControl
Example:
<Window.Resources>
<local:LicenceAgreementControl x:Key="LicenceAgreementControl" />
</Window.Resources>
<ContentControl Content="{Binding State}" DataContext="{Binding Source={StaticResource LicenceAgreementControl}}" />
Now provided you can see the view in design time load fine, we can update our method for design time only, else the problem is with the way the view model is being setup currently and need to sort that out firstly
^^Once above stuff works fine. To Make this as a design only feature, Remove the View Model being created as a Resource from xaml and can also remove explicit DataContext
set from the ContentControl
.
Now all you need is
d:DataContext="{d:DesignInstance Type=local:LicenceAgreementControl, IsDesignTimeCreatable=True}"
in the xaml file and you should be done (still need the ctor set the State property with the default View Model you want shown in the ContentControl
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.