[英]Dynamically changing content of ContentControl via trigger
I need to flip between two views dynamically based on a boolean flag in my ViewModel. 我需要基于ViewModel中的布尔值在两个视图之间动态切换。
I thought it would be as simple as: 我认为这很简单:
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{StaticResource View1}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsShowingView2}">
<Setter Property="Content" Value="{StaticResource View2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
However, View2
never gets displayed, even if IsShowingView2
is always true. 但是,即使
IsShowingView2
始终为true,也不会显示View2
。
Any ideas anyone? 有任何想法吗? All the examples I can find seem to be altering the ContentTemplate instead, but I have no need to do that.
我可以找到的所有示例似乎都在更改ContentTemplate,但是我不需要这样做。 I just want different content.
我只想要不同的内容。
You're not actually setting a value for the DataTrigger
您实际上并没有为
DataTrigger
设置值
<DataTrigger Binding="{Binding IsShowingView2}" Value="True">
<Setter Property="Content" Value="{StaticResource View2}" />
</DataTrigger>
Also check for binding errors in the output window. 还要在输出窗口中检查绑定错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.