仅使用 XAML 使用 DataTrigger 更改元素属性 [英] Change an element property with a DataTrigger using XAML only
问题描述
我正在尝试使用样式中的 DataTrigger 设置元素的属性.
I am trying to set a property of an element using DataTrigger in a style.
<Image x:Name="post_image1" Height="278" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding LatestFeed[1].PostImageURL}" MaxWidth="410" MaxHeight="410" Margin="0,0,50,0">
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger
Binding="{Binding post_image1.Source}"
Value="noimage">
<Setter Property="Image.Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
我想要发生的是,如果源值设置为noimage"(我将其设置为数据对象的一部分),则图像可见性属性设置为折叠.
What I want to happen is that if the Source Value is set to "noimage" (which I am setting as part of my data object) the Image Visibility property is set to Collapsed.
我想我已经接近了,我不确定我错过了什么.
I think I'm close, and I'm not sure what I'm missing.
推荐答案
由于您的 Style 直接应用于图像,因此 DataTrigger 中的 Bindings 使用当前图像的 DataContext,因此您可以以完全相同的方式引用该值在 Source 绑定中做了.
Since your Style is applied directly to the Image, the Bindings in the DataTrigger use the Current Image's DataContext, so you can reference the value the exact same way that you did in the Source binding.
<Image x:Name="post_image1" Height="278" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding LatestFeed[1].PostImageURL}" MaxWidth="410" MaxHeight="410" Margin="0,0,50,0">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger
Binding="{Binding LatestFeed[1].PostImageURL}"
Value="noimage">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
这篇关于仅使用 XAML 使用 DataTrigger 更改元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!