[英]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? 有什么办法可以使用
ValueConverter
或FallbackValue
或其他方法执行此操作?
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. 这些是我所做的步骤。
ContentControl
Content
Binding Value ( State
in your case) the default ViewModel to be shown. ContentControl
Content
绑定值(在您的情况下为State
),以显示默认的ViewModel。 Example: 例:
public LicenceAgreementControl() {
State = new NewViewModel();
}
d:DataContext
from your main xaml file. d:DataContext
。 DataContext
to your ContentControl
DataContext
分配给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
. 要将其作为仅设计功能,请从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.