开发者

Change an element property with a DataTrigger using XAML only

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}" MaxWidt开发者_运维技巧h="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>

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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜