WPF Setting Image.Source from DataTrigger
Asked Answered
G

2

10

I'm trying to define a DataTrigger for an Image element so that it shows a connected/disconnected image. I keep getting an Invalid PropertyDescriptor message. Any ideas?

<Image>
    <Image.Style>
      <Style>
        <Style.Triggers>
          <DataTrigger Binding="{Binding Source={x:Static my:Server.Instance}, Path=Connected, Mode=OneWay}"
                       Value="True">
            <Setter Property="Source"
                    Value="serverconnected.png"/>
          </DataTrigger>

        </Style.Triggers>
      </Style>
    </Image.Style>
</Image>
Garnish answered 19/1, 2010 at 17:58 Comment(0)
G
8

I just changed Property="Source" to Property="Image.Source" and it's working. Thanks everyone

Garnish answered 19/1, 2010 at 18:19 Comment(0)
P
2

Add a BitmapImage to your resources like so:

<BitmapImage x:Key="serverConnected" UriSource="serverconnected.png" />

And change your existing code to refer to it:

<Image>
<Image.Style>
  <Style>
    <Style.Triggers>
      <DataTrigger Binding="{Binding Source={x:Static my:Server.Instance}, Path=Connected, Mode=OneWay}"
                   Value="True">
        <Setter Property="Source"
                Value="{StaticResource serverConnected}"/>
      </DataTrigger>

    </Style.Triggers>
  </Style>
</Image.Style>

Protectionism answered 19/1, 2010 at 18:4 Comment(1)
This was a nice suggestion but was not the solution to the problem. +1 for the quick reply!Garnish

© 2022 - 2024 — McMap. All rights reserved.