如何在 C# WPF 代码中更改\设置按钮的背景图像?

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

How to change\set background image of a button in C# WPF code?

c#wpfimagebutton

提问by Jaz

I'm trying to change background image of my button to some other image and I encountered some errors. This is the code I have on my xaml:

我正在尝试将按钮的背景图像更改为其他图像,但遇到了一些错误。这是我在我的 xaml 上的代码:

    <Button x:Name="Button1" Width="200" Height="200" Content="Button1" Margin="0,0,0,400">
        <Button.Background>
            <ImageBrush **ImageSource ="Images/AERO.png"**  ></ImageBrush>
        </Button.Background>
    </Button>

and my cs:

和我的CS:

    private void Button1_Click_1(object sender, RoutedEventArgs e)
    {
        var brush = new ImageBrush();
        brush.ImageSource = new BitmapImage(new Uri("Images/AERO.png"));
        Button1.Background = brush;
    }

The error I have on my xaml is " The file 'Images\logo.png' is not part of the project or its 'Build Action' property is not set to 'Resource'. Can anyone help me explain, thanks

我在 xaml 上的错误是“文件 'Images\logo.png' 不是项目的一部分,或者它的 'Build Action' 属性未设置为 'Resource'。谁能帮我解释一下,谢谢

采纳答案by Walt Ritscher

In the build action, you can mark the image file as content or as resource. The syntax to use the image in an ImageBrush is different depending on which one you choose.

在构建操作中,您可以将图像文件标记为内容或资源。在 ImageBrush 中使用图像的语法因您选择的不同而不同。

Here is a image file marked as content.

这是一个标记为content的图像文件。

Image, marked as content

图片,标记为内容

To set the button background to this image use the following code.

要将按钮背景设置为此图像,请使用以下代码。

 var brush = new ImageBrush();
 brush.ImageSource = new BitmapImage(new Uri("Images/ContentImage.png",UriKind.Relative));
 button1.Background = brush;

Here is an image file marked as resource.

这是一个标记为resource的图像文件。

Image, marked as resource

图像,标记为资源

To set the button background to the resource image use the following code.

要将按钮背景设置为资源图像,请使用以下代码。

  Uri resourceUri = new Uri("Images/ResourceImage.png", UriKind.Relative);
  StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);

  BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
  var brush = new ImageBrush();
  brush.ImageSource = temp;

  button1.Background = brush;

回答by Kohanz

If it is not included, include the file "Image\Logo.png" in the project. Then set it's build action to "Resource" by visiting the properties tab for that file (right-click).

如果未包含,则在项目中包含文件“Image\Logo.png”。然后通过访问该文件的属性选项卡(右键单击)将其构建操作设置为“资源”。

Setting action to build resource

设置操作以构建资源

Also, I'm not sure what you're trying to do in the Click handler of the button. You are already setting the background image in the XAML. Unless you are setting it to another image in the Click handler, that code is not needed.

另外,我不确定您要在按钮的 Click 处理程序中执行什么操作。您已经在 XAML 中设置了背景图像。除非您将其设置为 Click 处理程序中的另一个图像,否则不需要该代码。

回答by Evren Gunay

I give a code snippet below, assing the style mentioned in code snippet to a button or to a toggle button, then you shall ve the changed background controlled totally by XAML...no other coding needed. I am not givin all the code just try to understand the logic behind ;)

我在下面给出了一个代码片段,将代码片段中提到的样式分配给按钮或切换按钮,然后您将完全由 XAML 控制更改的背景......不需要其他编码。我没有给出所有代码只是试图理解背后的逻辑;)

<Style x:Key="KeyStopButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                                  x:Name="Border"
                                  CornerRadius="1"
                                  BorderThickness="1">
                    <Border.Background>                            
                        <ImageBrush ImageSource= "..\Images\ButtonImages\StopButton.png"  Stretch="Uniform"/>
                    </Border.Background>                        
                    <Border.Effect>                            
                        <DropShadowEffect/>                                
                    </Border.Effect>
                </Border>
                <ControlTemplate.Triggers>                        
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="Border" Property="Border.Effect" Value="{x:Null}"/>                                                            
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="KeyPlayPauseButtonStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                                  x:Name="Border"
                                  CornerRadius="1"
                                  BorderThickness="1">
                    <Border.Background>
                        <ImageBrush ImageSource= "..\Images\ButtonImages\PlayButton.png"  Stretch="Uniform"/>
                    </Border.Background>
                    <Border.Effect>
                        <DropShadowEffect/>
                    </Border.Effect>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter TargetName="Border" Property="Border.Background">
                            <Setter.Value>
                                <ImageBrush ImageSource= "..\Images\ButtonImages\PauseButton.png" Stretch="Uniform"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>