简体   繁体   English

内容绑定到ValueConverter的后备或默认值

[英]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? 有什么办法可以使用ValueConverterFallbackValue或其他方法执行此操作?

XAML XAML

<ContentControl Content="{Binding State, 
              Converter={StaticResource InstallationStateToControlConverter}}" />

C# 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? 根据Viv的问题,我在XAML中添加了以下内容,它可以编译,但在设计器中仍然看不到任何内容?

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 假设所有运行时都很好,则可以查看ContentControl显示的模型并进行排序

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. 在构造方法中,分配ContentControl Content绑定值(在您的情况下为State ),以显示默认的ViewModel。

Example: 例:

public LicenceAgreementControl() {
  State = new NewViewModel();
}
  • Remove all occurrences of d:DataContext from your main xaml file. 从主xaml文件中删除所有出现的d:DataContext
  • Create the View Model as a plain old resource in xaml with some key and assign it as the DataContext to your ContentControl 使用一些密钥在xaml中将视图模型创建为普通的旧资源,并将其作为DataContext分配给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 Expression Blend中的开放式解决方案
  • Now In Visual Studio, Tools -> Attach to Process... -> "Select Blend in the List" -> Click Attach 现在在Visual Studio中,工具->附加到进程...->“在列表中选择混合”->单击附加
  • Switch back to blend. 切换回融合。 Close and open the xaml file. 关闭并打开xaml文件。 Your breakpoint in Visual Studio should be invoked 您在Visual Studio中的断点应被调用
  • Stepping through, I noticed an exception getting invoked which I could bypass with a IsInDesignState check. 逐步执行过程中,我注意到调用了一个异常,该异常可以通过IsInDesignState检查绕过。
  • 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) 如果没有例外,您应该在混合设计器中看到默认视图模型的视图加载(同样适用于Visual Studio设计器)

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 . 要将其作为仅设计功能,请从xaml中删除作为资源创建的视图模型,还可以从ContentControl删除显式的DataContext集。

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 ) 在xaml文件中,您应该完成操作(仍然需要ctor设置State属性为要在ContentControl显示的默认视图模型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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