[英]Fallback or default value for Content binding to ValueConverter
我有一个内容控件,可以根据当前状态显示动态内容。 一切正常,但是在设计时,我希望它显示默认状态。 有什么办法可以使用ValueConverter
或FallbackValue
或其他方法执行此操作?
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();
}
}
更新
根据Viv的问题,我在XAML中添加了以下内容,它可以编译,但在设计器中仍然看不到任何内容?
d:DataContext="{d:DesignInstance Type=local:LicenceAgreementControl, IsDesignTimeCreatable=True}"
好吧,我终于开始工作了,
这是评论中多项内容的结合。 希望这能为您解决。
假设所有运行时都很好,则可以查看ContentControl
显示的模型并进行排序
这些是我所做的步骤。
ContentControl
Content
绑定值(在您的情况下为State
),以显示默认的ViewModel。 例:
public LicenceAgreementControl() {
State = new NewViewModel();
}
d:DataContext
。 DataContext
分配给ContentControl
例:
<Window.Resources>
<local:LicenceAgreementControl x:Key="LicenceAgreementControl" />
</Window.Resources>
<ContentControl Content="{Binding State}" DataContext="{Binding Source={StaticResource LicenceAgreementControl}}" />
现在,只要您可以很好地看到设计时加载的视图,我们就可以仅在设计时更新我们的方法,否则问题在于当前正在建立视图模型的方式,需要首先进行分类
^^上面的东西工作正常。 要将其作为仅设计功能,请从xaml中删除作为资源创建的视图模型,还可以从ContentControl
删除显式的DataContext
集。
现在您需要的是
d:DataContext="{d:DesignInstance Type=local:LicenceAgreementControl, IsDesignTimeCreatable=True}"
在xaml文件中,您应该完成操作(仍然需要ctor设置State属性为要在ContentControl
显示的默认视图模型)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.