如何使用 DataTrigger 应用 DataTemplate 样式
我正在尝试应用基于 Binding 值的 ContentTemplate。问题是,它不起作用。
我有一个名为 TemplateA 的默认模板,然后我想显示基于数据绑定值的样式 - TemplateA 或 TemplateB。
如果我注释掉默认模板,则两个模板都不会被选择。
我检查了我的数据绑定值,该值正常。
你能看出我错在哪里吗?
这是 ListDataView,
<CollectionViewSource x:Key="ListDataView" />
它位于窗口的资源部分内,ListDataView 在代码中附加到 ObservableCollection。
<DataTemplate x:Key="TemplateA">
<TextBlock Text="Template A" />
</DataTemplate>
<DataTemplate x:Key="TemplateB">
<TextBlock Text="Template B" />
</DataTemplate>
<ContentControl x:Name="LISTINGCONTROLA">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource ListDataView}, Path=ListType}" Value="TEMPLATEA">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" />
</DataTrigger>
<DataTrigger Binding="{Binding Source={StaticResource ListDataView}, Path=ListType}" Value="TEMPLATEB">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateB}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
I'm trying to apply a ContentTemplate based on a Binding value. Problem is, it's not working.
I have a Default template, called TemplateA, I then want to display a style based on a databound value - being either TemplateA or TemplateB.
If I comment out the Default template, neither template is selected.
I have checked my databound value, the value is ok.
Can you see where I'm going wrong?
Here is the ListDataView
<CollectionViewSource x:Key="ListDataView" />
It's located within the Resources section of the Window, ListDataView is attached to an ObservableCollection within code.
<DataTemplate x:Key="TemplateA">
<TextBlock Text="Template A" />
</DataTemplate>
<DataTemplate x:Key="TemplateB">
<TextBlock Text="Template B" />
</DataTemplate>
<ContentControl x:Name="LISTINGCONTROLA">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource ListDataView}, Path=ListType}" Value="TEMPLATEA">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateA}" />
</DataTrigger>
<DataTrigger Binding="{Binding Source={StaticResource ListDataView}, Path=ListType}" Value="TEMPLATEB">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateB}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 WPF DataTemplateSelector 可能会有所帮助
http://www.switchonthecode.com/tutorials/wpf -教程如何使用数据模板选择器
Using a WPF DataTemplateSelector might be of help
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector
问题很可能是因为您的绑定指向
CollectionViewSource
本身的属性,而该类没有名为ListType
的属性,因此没有可绑定的值(因此始终使用默认值)。您尝试绑定的
ListType
属性在哪里?The problem is most likely because your binding is pointing to a property on the
CollectionViewSource
itself, and that class does not have a property namedListType
so there is no value to bind to (hence the default always being used).Where is the
ListType
property that you are trying to bind to?