views:

1003

answers:

3

I have a datatemplate containing an image that I want to be hidden if the the value of a property in a ViewModel is true. Can anyone tell me why the the xaml below does not work?

<Image x:Name="img" Source="..\Images\List_16.png" Margin="0,0,5,0">
  <Image.Style>
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding CurrentListHasPendingChanges}" Value="True">
          <Setter Property="Image.Visibility" Value="Hidden" />
        </DataTrigger>
        <DataTrigger Binding="{Binding CurrentListHasPendingChanges}" Value="False">
          <Setter Property="Image.Visibility" Value="Visible" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Image.Style>
</Image>
A: 

isn't that

<Setter Property="Visibility" Value="Hidden" />

?

I assume you use INotifyProptyChanged.

EDIT I did some Googling and I think you need to use some sort of template in order to make the trigger work.

eg.: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae2dbfb7-5dd6-4352-bfa1-53634289329d

http://www.thejoyofcode.com/Help%5FWhy%5Fcant%5FI%5Fuse%5FDataTriggers%5Fwith%5Fcontrols%5Fin%5FWPF.aspx

Natrium
Yes I do implement INotifyPropertyChanged.When I change the setter as per above, I get the following compile error:Cannot resolve the Style Property 'Visibility'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property
David Ward
Thanks for these, I'll go through them shortly
David Ward
A: 

Try removing "Image" part from Property="Image.Visibility" so you'll have:

<Setter Property="Visibility" Value="Hidden"/>

and add TargetType to your Style:

<Style TargetType="{x:Type Image}">
Stanislav Kniazev
A: 

I just did something similar using a ContentControl.

<ContentControl Content="{Binding CurrentListHasPendingChanges}">
  <ContentControl.ContentTemplate>
    <DataTemplate>
      <Image x:Name="img" Source="..\Images\List_16.png" Margin="0,0,5,0" Visibility="Hidden" />
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="False">
          <Setter Property="Image.Visibility" Value="Visible" />
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ContentControl.ContentTemplate>
</ContentControl>

From http://karlhulme.wordpress.com/2007/03/06/using-a-contentcontrol-and-datatemplate-to-indicate-new-andor-modified-data/

a_hardin