discorax
discorax

Reputation: 1487

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}" 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>

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.

Upvotes: 0

Views: 5093

Answers (1)

Rachel
Rachel

Reputation: 132558

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>

Upvotes: 1

Related Questions