gpt4 book ai didi

wpf - 无法从样式的数据触发器更改 ContentControl 的模板 - 数据触发器未触发

转载 作者:行者123 更新时间:2023-12-03 10:55:11 24 4
gpt4 key购买 nike

您好 WPF 爱好者,

我在用户控件中有一个内容控件,它将数据上下文设置为同一用户控件中 ListView 的选定项。 View 模型上的选定项属性称为 选择 .

这是内容控件的声明:

<ContentControl DataContext="{Binding Selection}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Template" Value="{StaticResource JobSnapShotProgDetail}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding bTandM}" Value="1">
<Setter Property="Template" Value="{StaticResource JobSnapShotProgDetail}"/>
</DataTrigger>
<DataTrigger Binding="{Binding bTandM}" Value="0">
<Setter Property="Template" Value="{StaticResource JobSnapShotTM_Detail}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>

模板在另一个资源文件中定义为 ControlTemplates,例如:
 <ControlTemplate x:Key="JobSnapShotProgDetail" >
...
</ControlTemplate >
<ControlTemplate x:Key="JobSnapShotTM_Detail" >
...
</ControlTemplate >

绑定(bind)很有效,这意味着模板的字段都显示来自 Selection 对象的正确数据。

我的问题:

我希望内容控件根据选择的 bTandM 属性的值使用两个不同的控件模板之一。

现在会发生什么:

当我更改 ListView 的选择时,这会更改 选择 对象,模板不会根据 的值进行更改选择的 bTandM 属性(一种 sql server 位数据类型)。是的 选择 对象实现 iNotifyPropertyChanged,并且 ControlTemplate 的字段绑定(bind)到 选择的属性都在输出窗口中显示正确的数据,没有任何绑定(bind)错误。

我试过的:

似乎数据触发器没有被代码“命中”。我试图通过添加 foo 而不是应该无法转换的数字来破坏数据触发器,但不会产生错误。 IE:
 <DataTrigger Binding="{Binding bTandM}" Value="foo">

这使我相信触发器由于某种原因没有触发。

问题:

有人可以帮我弄清楚为什么这个数据触发器没有效果。

提前致谢

JK

编辑1:

我尝试使用一种发现的技术 HERE ,但它也不起作用。数据触发器没有被触发。

将 DataTemplate 与针对内容控件的模板属性的 DataTrigger 一起使用的新尝试:
<ItemsControl ItemsSource="{Binding SelectionCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl x:Name="DetailControl" Template="{DynamicResource JobSnapShotProgDetail}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding bTandM}" Value="False">
<Setter TargetName="DetailControl" Property="Template" Value="{DynamicResource JobSnapShotProgDetail}" />
</DataTrigger>
<DataTrigger Binding="{Binding bTandM}" Value="1">
<Setter TargetName="DetailControl" Property="Template" Value="{DynamicResource JobSnapShotTM_Detail}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

同样,模板加载正常,并且在样式 setter 中设置的初始模板的属性正确显示,但模板不会根据对象的 bTandM bool 属性而更改。

最佳答案

好的,结果证明这是 NotifyPropertyChanged 的​​问题。

我的 View 模型的选择属性 实现 INPC,但问题是选择的基础类类型( vw_Job_Snapshot 是来自 sql server View 的映射实体)没有。

我必须在底层类中实现 iNPC,然后在我的 View 模型中使用选择属性的属性 setter 来通知特定的 bTandM 属性更改,如下所示:

    Public Property Selection As vw_Job_Snapshot
Get
Return Me._Selection
End Get
Set(ByVal value As vw_Job_Snapshot)
Me._Selection = value

''Notify of specific property changed
value.OnPropertyChanged("bTandM")

_Selection = value
RaisePropertyChanged(JobSnapshotSelectedPropertyName)
End Set
End Property

在此之后,我认为以下代码完美运行:
<ContentControl DataContext="{Binding Selection}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Template" Value="{DynamicResource JobSnapShotTM_Detail}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding bTandM}" Value="False">
<Setter Property="Template" Value="{DynamicResource JobSnapShotProgDetail}"/>
</DataTrigger>
<DataTrigger Binding="{Binding bTandM}" Value="True">
<Setter Property="Template" Value="{DynamicResource JobSnapShotTM_Detail}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>

感谢所有帮助过的人

关于wpf - 无法从样式的数据触发器更改 ContentControl 的模板 - 数据触发器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23730717/

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