我想根据动态资源的值切换控件样式。
<Image x:Name="leftImg" Stretch="Fill" Style="{StaticResource BookendImageStyle}" >
<Image.Triggers>
<DataTrigger Binding="{DynamicResource BlackAndWhite}" Value="True">
<Setter TargetName="leftImg" Property="Style" Value="{StaticResource BookendImageStyleBW}" />
</DataTrigger>
</Image.Triggers>
</Image>
这编译得很好但是在运行时我得到以下异常:
A 'DynamicResourceExtension' cannot be set on the 'Binding' property of type 'DataTrigger'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.
我想尝试做什么?我的语法错了吗?我应该采用不同的方式吗?
修改的 我已将代码更改为:
<DataTemplate x:Key="ViasTemplate">
<StackPanel Orientation="Horizontal">
<Border Padding="{Binding ElementName=templateTest, Path=DataContext.GridRow.LeftBondIndent}" Width="50">
<Image x:Name="leftImg" Stretch="Fill" Style="{StaticResource BookendImageStyle}" >
<Image.Triggers>
<DataTrigger Binding="{Binding Source={StaticResource BlackAndWhite}}" Value="True">
<Setter TargetName="leftImg" Property="Style" Value="{StaticResource BookendImageStyleBW}" />
</DataTrigger>
</Image.Triggers>
</Image>
但现在得到这个例外:
Triggers collection members must be of type EventTrigger
由于
答案 0 :(得分:1)
<Image x:Name="leftImg" Stretch="Fill" Style="{StaticResource BookendImageStyle}" >
<Image.Triggers>
<DataTrigger Binding="{Binding Source={DynamicResource BlackAndWhite}}" Value="True">
<Setter TargetName="leftImg" Property="Style" Value="{StaticResource BookendImageStyleBW}" />
</DataTrigger>
</Image.Triggers>
</Image>
当然你应该将这个触发器移动到图像样式。 DataTrigger只能在样式或模板中定义