如何仅在xaml中单击按钮时显示图像可见性

时间:2013-04-17 17:21:43

标签: wpf xaml

我正在尝试更改按钮单击时图像的可见性,但随后图像消失并将其可见性更改回隐藏状态。基本上,保存图像让用户知道保存成功。我想只在xaml中这样做。有什么建议吗?

代码:

<Image x:Name="SavedImage"
       Source="/check.ico">
  <Image.Resources>
    <Style TargetType="Image">
      <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=SaveDocumentsButton, Path=IsPressed}"
                     Value="True">
          <Setter Property="Visibility"
                  Value="Visible"></Setter>
        </DataTrigger>
        <Trigger Property="Image.Visibility"
                 Value="Visible">
          <Trigger.EnterActions>
            <BeginStoryboard>
              <Storyboard x:Name="saveStoryBoard">
                <DoubleAnimation x:Name="z"
                                 BeginTime="0:0:0"
                                 Duration="0:0:3"
                                 From="1.0"
                                 To="0.0"
                                 Storyboard.TargetProperty="Opacity" />
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Image.Resources>
</Image>
<Button x:Name="SaveDocumentsButton"
        Style="{StaticResource SaveButtonWhite}"
        Command="{Binding SaveUnitCommand}"
        HorizontalAlignment="Right">
</Button>

1 个答案:

答案 0 :(得分:0)

这应该很好用:

<Grid>
  <Image x:Name="SavedImage"
         Source="http://i.msdn.microsoft.com/dn166920.cover_lrg(en-us,MSDN.10).jpg">
    <Image.Resources>
      <Style TargetType="Image">
        <Style.Triggers>
          <DataTrigger Binding="{Binding ElementName=SaveDocumentsButton, Path=IsPressed}"
                       Value="True">
            <Setter Property="Opacity"
                    Value="1"></Setter>

            <DataTrigger.EnterActions>

              <BeginStoryboard>
                <Storyboard x:Name="saveStoryBoard">
                  <DoubleAnimation x:Name="z"
                                   BeginTime="0:0:0"
                                   Duration="0:0:3"
                                   From="1.0"
                                   To="0.0"
                                   Storyboard.TargetProperty="Opacity" />
                </Storyboard>
              </BeginStoryboard>
            </DataTrigger.EnterActions>
          </DataTrigger>

        </Style.Triggers>
      </Style>
    </Image.Resources>
  </Image>
  <Button x:Name="SaveDocumentsButton"
          Content="TEST"
          HorizontalAlignment="Right">
  </Button>
</Grid>