Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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 - Fatal编程技术网

C# 按钮图像更改

C# 按钮图像更改,c#,wpf,C#,Wpf,我想在用户每次按下按钮时更改按钮的背景图像。 我已将ImageSource设置为我的图片。当鼠标位于按钮上方时,我的按钮不会显示图像。如何设置图片,以便即使鼠标悬停在按钮上,按钮仍带有图像 <Button x:Name="recordButton" Content="" HorizontalAlignment="Left" Height="50" Margin="60,45,0,0" VerticalAlignment="Top" Width="50" Click="rec

我想在用户每次按下按钮时更改按钮的背景图像。 我已将ImageSource设置为我的图片。当鼠标位于按钮上方时,我的按钮不会显示图像。如何设置图片,以便即使鼠标悬停在按钮上,按钮仍带有图像

        <Button x:Name="recordButton" Content="" HorizontalAlignment="Left" Height="50" Margin="60,45,0,0" VerticalAlignment="Top" Width="50" Click="recordButton_Click">
        <Button.Background>
            <ImageBrush ImageSource="play.png"/>
        </Button.Background>
    </Button>
我已经尝试过了,但是VS找不到
BitmapImage image=Properties.Resources.stop

救命啊

我建议不要将背景设置为图像,而是将内容设置为图像

<Button x:Name="recordButton" HorizontalAlignment="Left" Height="50" Margin="60,45,0,0" VerticalAlignment="Top" Width="50" Click="recordButton_Click">
    <Image Source="play.png"/>
</Button>

如果要使用资源文件,请参见此

有一些更改图片的方法:

  • 重新定义按钮控件模板并使用

  • 重新定义控件模板并使用s

  • 您可以尝试将按钮的内容绑定到它的
    MouseOver
    property:when is mouse over隐藏一个图像并显示另一个图像


  • 嗯。但是换照片有什么帮助呢?谢谢。但是现在按下按钮后,它不再显示任何图像。因此它没有帮助:(确保“Images/stop.png”是图像的正确位置,并且设置为作为资源编译(项目属性)
    <Button x:Name="recordButton" HorizontalAlignment="Left" Height="50" Margin="60,45,0,0" VerticalAlignment="Top" Width="50" Click="recordButton_Click">
        <Image Source="play.png"/>
    </Button>
    
    (recordButton.Content as Image).Source = new BitmapImage(new Uri("Images/stop.png", UriKind.Relative));