WPF从DataTrigger设置Image.Source

时间:2010-01-19 17:58:01

标签: wpf image styles datatrigger

我正在尝试为DataTrigger元素定义Image,以便显示已连接/已断开连接的图像。我一直收到Invalid PropertyDescriptor消息。有什么想法吗?

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

2 个答案:

答案 0 :(得分:8)

我刚刚将Property =“Source”更改为Property =“Image.Source”并且它正在运行。谢谢大家

答案 1 :(得分:2)

将BitmapImage添加到您的资源中,如下所示:

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

并更改现有代码以引用它:

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