Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当按钮在c#中单击时,你能将其变成图像吗?_C#_Wpf_Uwp - Fatal编程技术网

当按钮在c#中单击时,你能将其变成图像吗?

当按钮在c#中单击时,你能将其变成图像吗?,c#,wpf,uwp,C#,Wpf,Uwp,尝试在单击按钮时更改按钮的背景图像,或在单击时将按钮更改为图像 var brush = new ImageBrush { ImageSource = new BitmapImage(new Uri("image1.jpg", UriKind.Relative)) }; Button1.Background = brush; 如果您正在使用UWP或WPF,您可以这样做(这正是您已经拥有的):

尝试在单击按钮时更改按钮的背景图像,或在单击时将按钮更改为图像

 var brush = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri("image1.jpg", UriKind.Relative))
            };
            Button1.Background = brush;

如果您正在使用UWP或WPF,您可以这样做(这正是您已经拥有的):


或如此处所述:

无im使用wpf@AnirudhShankar将图像设置在按钮的相同位置,将图像可见属性值更改为false,单击按钮时,按钮的可见值为false,图像的可见值为true。对于WPF,您可以使用与UWP完全相同的代码。两者都有XAML和代码隐藏。否则,这里显示的代码就是OP已经拥有的代码。WinForms部分完全不相关。谢谢@clemens,你是对的,我编辑了我的答案。这回答了你的问题吗?WPF/UWP和XAML的设计考虑了MVVM模式。虽然您可以使用其他方法,但这样做会损失大约90%的能量,并且在其他每个角落都会遇到问题。几年前,我写了一篇关于MVVM的介绍:|这张图片像是重新反映了两件事之一。1) 设置一个Bool 2)一系列设置,你可以通过。在这种情况下,这是一个基本的绑定问题,加上可能的转换器。
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("uri to image"));
btn.Background = brush1;