WPF 触发器绑定到 MVVM 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1795476/
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-09-08 21:15:36  来源:igfitidea点击:

WPF Trigger binding to MVVM property

wpfbindingtriggers

提问by David Ward

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?

我有一个包含图像的数据模板,如果 ViewModel 中的属性值为 true,我希望将其隐藏。谁能告诉我为什么下面的 xaml 不起作用?

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

采纳答案by Natrium

isn't that

那不是

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

?

?

I assume you use INotifyProptyChanged.

我假设您使用 INotifyProptyChanged。

EDITI 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: //social.msdn.microsoft.com/Forums/en-US/wpf/thread/ae2dbfb7-5dd6-4352-bfa1-53634289329d

http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx

http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx

回答by Stanislav Kniazev

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

尝试从 Property="Image.Visibility" 中删除 "Image" 部分,这样你就会有:

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

and add TargetType to your Style:

并将 TargetType 添加到您的样式:

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

回答by a_hardin

I just did something similar using a ContentControl.

我只是使用 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/

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

回答by Adel

In my opinion we not need to use Triggers, with only the Binding it works well. To make binding to a property model, you can use BooleanToVisibilityConverter Is declared as follows:

在我看来,我们不需要使用 Triggers,只需要 Binding 就可以了。要绑定到属性模型,可以使用 BooleanToVisibilityConverter 声明如下:

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>

And how to use it is simple, just point to the key stated above:

而且怎么使用也很简单,直接指向上面说的key即可:

<Image HorizontalAlignment="Left" Height="16"  VerticalAlignment="Center" Width="16" 
       Visibility="{Binding HasError, Converter={StaticResource BooleanToVisibilityConverter}}"
       Source="/myPath;component/Resources/Images/image1.png"/>

The property in the ViewModel:

ViewModel 中的属性:

private bool _hasError = false;
    public bool HasError
    {
        get { return !string.IsNullOrEmpty(_messageError); }
        set 
        {
            _hasError = value;                
            this.NotifyOfPropertyChange(() => this.HasError);
        }
    }