本文介绍了如何在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中将样式触发器应用于数据模板
发布评论