显示后关闭wpf中的图像

本文关键字:图像 wpf 显示 | 更新日期: 2023-09-27 18:26:08

我做了一个简单的按钮,当你按下它时,会出现一个图像。我已经制作了image.Stretch = "Fill",所以我现在的问题是:

当图像出现时,我该如何返回?如何在不停止程序的情况下再次关闭图像。

我在button_click下的代码很简单:

private void Button_Click(object sender, RoutedEventArgs e)
{
   nr1.Visibility = Visibility.Visible;
}

我想以某种方式再次隐藏图像。。

这是我的xaml代码

<Button Name="Button1" Content="1" Click="Button_Click" />
<Image Name="nr1" Visibility="Hidden" Source="(ImageSource etc.)" "      Stretch="Fill"/>

谢谢!

显示后关闭wpf中的图像

您可以这样做:

<Grid>
    <Button Name="Button1" Content="1" Click="Button1_OnClick"/>
    <Button Name="nr1"  Visibility="Hidden" Click="Nr1_OnClick">
        <Button.Template>
            <ControlTemplate>
                <Image  Source="YourImage" Stretch="Fill" />
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>

private void Button1_OnClick(object sender, RoutedEventArgs e)
    {
        if (nr1.Visibility == Visibility.Visible)
        {
            nr1.Visibility = Visibility.Hidden;
            Button1.Visibility = Visibility.Visible;
        }
        else
        {
            nr1.Visibility = Visibility.Visible;
            Button1.Visibility = Visibility.Hidden;
        }
    }
    private void Nr1_OnClick(object sender, RoutedEventArgs e)
    {
        if (Button1.Visibility == Visibility.Visible)
        {
            Button1.Visibility = Visibility.Hidden;
            nr1.Visibility = Visibility.Visible;
        }
        else
        {
            nr1.Visibility = Visibility.Hidden;
            Button1.Visibility = Visibility.Visible;
        }
    }

我相信这与Sinatr的建议类似。我希望你明白。