gpt4 book ai didi

wpf - 使用 DataTrigger 将不同的 View 应用到同一个 ViewModel

转载 作者:行者123 更新时间:2023-12-01 10:49:41 25 4
gpt4 key购买 nike

我有一个 ViewModel,我在其中创建了一个 bool DisplaySummary 属性。如果为真,将使用 SummaryView 来呈现该 ViewModel,否则将使用 DatailedView

我不确定我应该如何从这里开始:

<DataTemplate DataType="{x:Type vm:AwesomeViewModel}">
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style>
#### WHAT I SHOULD PUT HERE?
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>

<DataTemplate x:Key="SummaryTemplate">
<vw:SummaryViewScreen />
</DataTemplate>

<DataTemplate x:Key="DetailedTemplate">
<vw:DetailedViewScreen />
</DataTemplate>

编辑:起初我尝试使用 DataTemplateSelector,但由于它不响应 PropertyChanged,我不得不使用 DataTriggers。

最佳答案

使用DataTrigger切换ContentTemplate:

<DataTemplate DataType="{x:Type vm:AwesomeViewModel}">
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate"
Value="{StaticResource DetailedTemplate}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding DisplaySummary}" Value="True">
<Setter Property="ContentTemplate"
Value="{StaticResource SummaryTemplate}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>

关于wpf - 使用 DataTrigger 将不同的 View 应用到同一个 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com