使用触发器 WPF MVVM 更改图像 [英] Change image using trigger WPF MVVM

查看:35
本文介绍了使用触发器 WPF MVVM 更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能并不明智,但我似乎无法让它发挥作用.我有一个视图模型,它公开了一个名为 bool NotFound 的属性,我想将它绑定到一个触发器,以便当它更改我的控件上的图像时.

This may be a no brainier but I just can't seem to get it to work. I have a view model that exposes a property called bool NotFound I would like to bind that to a trigger so that when it changes an image on my control changes.

这是我用作其中一个视图模型的数据模板的 xaml.

This is the xaml that I am using as a data template for one of my view models.

<DataTemplate DataType="{x:Type local:TabFileViewModel}">
        <StackPanel Orientation="Horizontal">
              <Image Width="16" Height="16" Margin="3,0" Source="ImageTabFile.PNG" />
              <TextBlock Text="{Binding Name}" ToolTip="{Binding FullPath}"/>
       </StackPanel>
</DataTemplate>

我希望能够将 绑定到 NotFound 属性并更改图像源.

I would like to be able to bind the to the NotFound property and change the image source.

推荐答案

一切都好,我想出来了.

It's all good I figured it out.

<DataTemplate DataType="{x:Type local:TabFileViewModel}">
       <StackPanel Orientation="Horizontal">
         <Image Width="16" Height="16" Margin="3,0">
             <Image.Style>
                 <Style TargetType="{x:Type Image}">
                 <Style.Triggers>
                      <DataTrigger Binding="{Binding NotFound}" Value="false">
                          <Setter Property="Source" Value="ImageTabFile.PNG"/>
                      </DataTrigger>
                      <DataTrigger Binding="{Binding NotFound}" Value="true">
                          <Setter Property="Source" Value="ImageErrorTabFile.PNG"/>
                      </DataTrigger>
                   </Style.Triggers>
              </Style>
           </Image.Style>
     </Image>
</DataTemplate> 

这篇关于使用触发器 WPF MVVM 更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆