如何在WPF中将样式触发器应用于数据模板

编程入门 行业动态 更新时间:2024-10-21 17:28:15
本文介绍了如何在WPF中将样式触发器应用于数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下内容。

<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" Margin="85,2,0,2"> <ComboBox.ItemTemplate> <DataTemplate> <TextBox AcceptsReturn="True" Width="200" Height="100"/> <DataTemplate.Resources> <Style TargetType="{x:Type TextBox}"> <Setter Property="IsReadOnly" Value="True"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="False"/> </Style.Triggers> </Setter> </Style> </DataTemplate.Resources> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

问题是您无法应用Style.Trigger,就像我尝试在内部进行的操作一个数据模板。所以我的问题是,您将如何应用创建触发器,以便DataTemplate上的属性基于父级更改?

The problem is that you can't apply a Style.Trigger like I'm trying to do inside a DataTemplate. So my question is how would you apply create a trigger so that a property on the DataTemplate changes based on the parent?

最终解决方案:

由于有一些问题,我接受了Souvik给我的东西并进行了修复。这是最终结果。

I took what Souvik gave me and fixed it up since there were a few problems. Here is the end result.

<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" DisplayMemberPath="Value" Margin="85,2,0,2"> <ComboBox.ItemTemplate> <DataTemplate> <TextBox AcceptsReturn="True" Width="200" Height="100" Text="{Binding Path=Value}"/> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=IsEditable}" Value="False"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ComboBox.ItemTemplate> <ComboBox.Resources> <Style TargetType="{x:Type ComboBox}"> <Setter Property="IsEditable" Value="True"/> <Style.Triggers> <Trigger Property="IsDropDownOpen" Value="True" > <Setter Property="IsEditable" Value="False"/> </Trigger> </Style.Triggers> </Style> </ComboBox.Resources>

推荐答案

使用DataTemplate触发器代替样式触发器:

Have DataTemplate trigger instead of Style trigger:

<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" Margin="85,2,0,2"> <ComboBox.ItemTemplate> <DataTemplate> <TextBox AcceptsReturn="True" Width="200" Height="100"/> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="False"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

更多推荐

如何在WPF中将样式触发器应用于数据模板

本文发布于:2023-10-28 01:10:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   应用于   中将   样式   模板

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!