简体   繁体   中英

Fallback or default value for Content binding to ValueConverter

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.

  • Make sure to have an Argument-less Constructor for the View Model
  • In the Constructor assign ContentControl Content Binding Value ( State in your case) the default ViewModel to be shown.

Example:

public LicenceAgreementControl() {
  State = new NewViewModel();
}
  • Remove all occurrences of d:DataContext from your main xaml file.
  • Create the View Model as a plain old resource in xaml with some key and assign it as the DataContext to your ContentControl

Example:

<Window.Resources>
  <local:LicenceAgreementControl x:Key="LicenceAgreementControl" />
</Window.Resources>
<ContentControl Content="{Binding State}" DataContext="{Binding Source={StaticResource LicenceAgreementControl}}" />
  • Put a Breakpoint in your View Model Constructor
  • Open Solution in Expression Blend
  • Now In Visual Studio, Tools -> Attach to Process... -> "Select Blend in the List" -> Click Attach
  • Switch back to blend. Close and open the xaml file. Your breakpoint in Visual Studio should be invoked
  • Stepping through, I noticed an exception getting invoked which I could bypass with a IsInDesignState check.
  • If you have no exception you should see the default view model's view load up in the blend designer (same applies to Visual Studio designer)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM