C# 使用触发器 WPF MVVM 更改图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1786477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:49:07  来源:igfitidea点击:

Change image using trigger WPF MVVM

c#wpfxamlmvvm

提问by Nathan W

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 NotFoundI would like to bind that to a trigger so that when it changes an image on my control changes.

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

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

这是我用作视图模型之一的数据模板的 xaml。

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

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

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

采纳答案by Nathan W

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="Image\TabFile.PNG"/>
                      </DataTrigger>
                      <DataTrigger Binding="{Binding NotFound}" Value="true">
                          <Setter Property="Source" Value="Image\ErrorTabFile.PNG"/>
                      </DataTrigger>
                   </Style.Triggers>
              </Style>
           </Image.Style>
     </Image>
</DataTemplate> 

回答by bitbonk

<DataTemplate DataType="{x:Type local:TabFileViewModel}">
        <StackPanel Orientation="Horizontal">
              <Grid>
                  <Image x:Name="a" Width="16" Height="16" Margin="3,0" Source="Image\NotFounds.PNG" />
                  <Image x:Name="b" Width="16" Height="16" Margin="3,0" Source="Image\TabFile.PNG" />
                </Grid>
              <TextBlock Text="{Binding Name}" ToolTip="{Binding FullPath}"/>
       </StackPanel>
       <DataTemplate.Triggers>
            <DataTrigger Binding={Binding NotFound} Value="true">
                  <Setter TargetName="a" TargetProperty="Visibility" Value="Visible" />
                  <Setter TargetName="b" TargetProperty="Visibility" Value="Hidden" />
            </DataTrigger>
            <DataTrigger Binding={Binding NotFound} Value="false">
                  <Setter TargetName="a" TargetProperty="Visibility" Value="Hidden" />
                  <Setter TargetName="b" TargetProperty="Visibility" Value="Visible" />
            </DataTrigger>
       </DataTemplate.Triggers>
</DataTemplate>